Advertisement
Guest User

usaco 1042 fcolor

a guest
Apr 4th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define pii pair<int,int>
  4. #define pll pair<ll,ll>
  5. #define pli pair<ll,int>
  6. #define pil pair<int,ll>
  7. #define fi first
  8. #define se second
  9. #define inf (INT_MAX/2-1)
  10. #define infl (1LL<<60)
  11. #define vi vector<int>
  12. #define vl vector<ll>
  13. #define pb push_back
  14. #define sz(a) (int)(a).size()
  15. #define all(a) begin(a),end(a)
  16. #define y0 y5656
  17. #define y1 y7878
  18. #define aaa system("pause");
  19. #define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
  20. #define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
  21. #define dbgs(x) cerr<<(#x)<<"[stl]: ";for(auto _:x)cerr<<_<<' ';cerr<<'\n',aaa
  22. #define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
  23. #define maxn 200000
  24.  
  25. using namespace std;
  26.  
  27. ifstream fin ("fcolor.in");
  28. ofstream fout ("fcolor.out");
  29.  
  30. int n;
  31. vi g[maxn+5];
  32. int sol[maxn+5];
  33. queue<int> qu;
  34.  
  35. struct DSU {
  36.   DSU () {}
  37.   vi sef[maxn+5]; ///sef[i] = pe cine a mancat i, de fapt sz(sef[i]) = sy[i]
  38.   int tata[maxn+5]; ///tata[i] = de cine a fost mancat i
  39.   void rest () {
  40.     for (int i = 1; i <= n; i++) {
  41.       tata[i] = i;
  42.       sef[i].pb(i);
  43.     }
  44.   }
  45.   int afla (int x) {
  46.     int r = x, aux;
  47.     while (tata[r] != r) r = tata[r];
  48.     while (x != r) {
  49.       aux = tata[x];
  50.       tata[x] = r;
  51.       x = aux;
  52.     }
  53.     return r;
  54.   }
  55.   void uneste (int a, int b) {
  56.     a = afla(a); b = afla(b);
  57.     if (a == b) return;
  58.     if (sz(sef[a]) < sz(sef[b])) swap(a, b);
  59.     for (int x: sef[b]) {
  60.       tata[x] = a; ///<- de asta nu trebuie sa te ocupi si de gt V
  61.       sef[a].pb(x);
  62.     }
  63.     sef[b].clear();
  64.     g[a].insert(g[a].end(), g[b].begin(), g[b].end());
  65.     g[b].clear();
  66.     if (sz(g[a]) > 1) qu.push(a);
  67.   }
  68. };
  69.  
  70. DSU dsu;
  71.  
  72. int main () {
  73.   int m; fin >> n >> m;
  74.   int i, j, z, a, b;
  75.   for (i = 0; i < m; i++) {
  76.     fin >> a >> b;
  77.     g[a].pb(b);
  78.   }
  79.   dsu.rest();
  80.   for (i = 1; i <= n; i++)
  81.     if (sz(g[i]) > 1) qu.push(i);
  82.   while (!qu.empty()) {
  83.     int nod = qu.front();
  84.     if (sz(g[nod]) > 1) {
  85.       int nn = g[nod].back();
  86.       g[nod].pop_back();
  87.       if (dsu.afla(nn) != dsu.afla(g[nod].back())) dsu.uneste(nn, g[nod].back());
  88.     } else qu.pop();
  89.   }
  90.   for (z = 1, i = 1; i <= n; i++) {
  91.     j = dsu.afla(i);
  92.     if (sol[j] == 0) sol[j] = z++;
  93.     sol[i] = sol[j];
  94.   }
  95.   for (i = 1; i <= n; i++) fout << sol[i] << '\n';
  96.   fin.close(); fout.close();
  97.   return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement