Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<algorithm>
- #include<utility>
- #include<queue>
- #include<stack>
- using namespace std;
- class UF {
- int *id, cnt, *sz;
- public:
- // Create an empty union find data structure with N isolated sets.
- UF(int N) {
- cnt = N; id = new int[N]; sz = new int[N];
- for (int i = 0; i<N; i++) id[i] = i, sz[i] = 1; }
- ~UF() { delete[] id; delete[] sz; }
- // Return the id of component corresponding to object p.
- int find(int p) {
- int root = p;
- while (root != id[root]) root = id[root];
- while (p != root) { int newp = id[p]; id[p] = root; p = newp; }
- return root;
- }
- // Replace sets containing x and y with their union.
- void merge(int x, int y) {
- int i = find(x); int j = find(y); if (i == j) return;
- // make smaller root point to larger one
- if (sz[i] < sz[j]) { id[i] = j, sz[j] += sz[i]; }
- else { id[j] = i, sz[i] += sz[j]; }
- cnt--;
- }
- // Are objects x and y in the same set?
- bool connected(int x, int y) { return find(x) == find(y); }
- // Return the number of disjoint sets.
- int count() { return cnt; }
- };
- int N, K, T;
- vector<pair<int, int> > v[50002];
- vector<bool> vis;
- int res;
- UF ufind = UF(50004);
- void dfs(int z, int w) {
- stack<int> s;
- s.push(z);
- while(!s.empty()) {
- int n = s.top();
- s.pop();
- if(!vis[n]) {
- vis[n] = true;
- for(int i=0;i<v[n].size();i++) {
- if(v[n][i].second >= w && !ufind.connected(n, v[n][i].first)) {
- ufind.merge(n, v[n][i].first);
- res--;
- s.push(v[n][i].first);
- }
- }
- }
- }
- }
- int main() {
- std::ios_base::sync_with_stdio (false);
- cin>>N>>K>>T;
- res = N;
- N = N+1; //usa indici partendo da 1
- vis.resize(N);
- for(int i=0;i<K;i++) {
- int a,b, V;
- cin>>a>>b>>V;
- v[a].push_back(make_pair(b,V));
- v[b].push_back(make_pair(a,V));
- }
- int sol[100002];
- int ord[100002];
- int wi[100002];
- for(int t=0;t<T;t++) {
- int w;
- cin>>w;
- ord[t] = w;
- wi[t] = w;
- }
- sort(wi, wi + T);
- for(int t=0;t<T;t++) {
- int w = wi[T - t - 1];
- fill(vis.begin(), vis.end(), false);
- for(int i=1;i<N;i++) {
- if(!vis[i]) {
- dfs(i, w);
- }
- }
- sol[w] = res;
- }
- for(int t=0;t<T;t++) cout<<sol[ord[t]]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment