Advertisement
Guest User

usaco 838 milkorder wa

a guest
Feb 15th, 2020
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 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 fi first
  7. #define se second
  8. #define inf (INT_MAX/2-1)
  9. #define infl (1LL<<60)
  10. #define vi vector<int>
  11. #define pb push_back
  12. #define sz(a) (int)(a).size()
  13. #define all(a) begin(a),end(a)
  14. #define y0 y5656
  15. #define y1 y7878
  16. #define aaa system("pause");
  17. #define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
  18. #define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
  19. #define dbgs(x) cerr<<(#x)<<"[stl]: ";for(int _:x)cerr<<_<<' ';cerr<<'\n',aaa
  20. #define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
  21. #define maxn 100000
  22. #define maxm 50000
  23.  
  24. using namespace std;
  25.  
  26. ifstream fin ("milkorder.in");
  27. ofstream fout ("milkorder.out");
  28.  
  29. int n, m;
  30. vi sir[maxm+5];
  31. vector<pii> g[maxn+5]; ///fi=unde duci se=din ce sir este mch
  32. priority_queue<int,vi,greater<int> > pq;
  33. int grad[maxn+5];
  34.  
  35. vi can (int nr) {
  36.   fill(all(grad), 0);
  37.   while (!pq.empty()) pq.pop();
  38.   for (int i = 0; i < n; i++)
  39.     for (pii nn: g[i])
  40.       if (nn.se <= nr) grad[nn.fi]++;
  41.   for (int i = 0; i < n; i++)
  42.     if (grad[i] == 0) pq.push(i);
  43.   vi ans;
  44.   while (!pq.empty()) {
  45.     int nod = pq.top(); pq.pop();
  46.     ans.pb(nod);
  47.     for (pii nn: g[nod])
  48.       if (nn.se <= nr) {
  49.         grad[nn.fi]--;
  50.         if (grad[nn.fi] == 0) pq.push(nn.fi);
  51.       }
  52.   }
  53.   return ans;
  54. }
  55.  
  56. int main () {
  57.   fin >> n >> m;
  58.   int i, j, x, z, pas;
  59.   for (i = 0; i < m; i++) {
  60.     fin >> z;
  61.     for (j = 0; j < z; j++) fin >> x, sir[i].pb(x-1);
  62.   }
  63.   for (i = 0; i < m; i++)
  64.     for (j = 0; j < sz(sir[i])-1; j++)
  65.   g[sir[i][j]].pb({sir[i][j+1], i});
  66.   vi lst, cur;
  67.   for (z = -1, pas = (1<<17); pas > 0; pas >>= 1)
  68.     if (z+pas < m) {
  69.       cur = can(z+pas);
  70.       if (sz(cur) == n) lst = cur;
  71.       z += pas;
  72.     }
  73.   for (int aa: lst) fout << aa+1 << ' ';
  74.   fin.close(); fout.close();
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement