蓝桥杯2022省赛I题 - 推导部分和 -带权并查集datastructure
作者:互联网
#include <bits/stdc++.h> #define dbg(x) std::cerr << #x << "=" << x << "\n" using i64 = long long; const int N = 1e5 + 9; i64 val[N]; int fa[N]; int find(int x){ if(fa[x] == x) return x; int oldfa = fa[x]; fa[x] = find(fa[x]); val[x] += val[oldfa]; return fa[x]; } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m, q; std::cin >> n >> m >> q; for(int i = 1; i <= n ; i++){ fa[i] = i; } for(int i = 1; i <= m ; i++){ i64 L,R,S; std::cin >> L >> R >> S; L--; int fx = find(L), fy = find(R); fa[fy] = fx; val[fy] = val[L] - val[R] - S; } while(q--){ i64 L, R; std::cin >> L >> R; L--; int fx = find(L), fy = find(R); if(fx != fy) std::cout<< "UNKNOWN" << "\n"; else std::cout<< val[L] - val[R] << "\n"; } return 0; }
标签:std,val,fx,fy,查集,蓝桥,int,datastructure,find 来源: https://www.cnblogs.com/zrzsblog/p/16493824.html