Alex_tz307

semafor2 - 100p

Dec 3rd, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("semafor2.in");
  6. ofstream fout("semafor2.out");
  7.  
  8. int N, M, cnt;
  9. vector<vector<int>> G, GT;
  10. vector<int> viz, ctc, sz;
  11. stack<int> S;
  12.  
  13. void dfs1(int node) {
  14.     viz[node] = 1;
  15.     for(int next : G[node])
  16.         if(!viz[next])
  17.             dfs1(next);
  18.     S.emplace(node);
  19. }
  20.  
  21. void dfs2(int node) {
  22.     viz[node] = 2;
  23.     ctc[node] = cnt;
  24.     ++sz[cnt];
  25.     for(int next : GT[node])
  26.         if(viz[next] == 1)
  27.             dfs2(next);
  28. }
  29.  
  30. int main() {
  31.     fin >> N >> M;
  32.     G.resize(N << 1 | 1), GT.resize(N << 1 | 1);
  33.     while(M--) {
  34.         int a, b;
  35.         bool c;
  36.         fin >> a >> b >> c;
  37.         if(c) {
  38.             G[a].emplace_back(b + N);
  39.             GT[b + N].emplace_back(a);
  40.         }
  41.         else {
  42.             G[a + N].emplace_back(b);
  43.             GT[b].emplace_back(a + N);
  44.         }
  45.     }
  46.     viz.resize(N << 1 | 1);
  47.     for(int i = 1; i <= (N << 1); ++i)
  48.         if(!viz[i])
  49.             dfs1(i);
  50.     ctc.resize(N << 1 | 1), sz.resize(N << 1 | 1);
  51.     while(!S.empty()) {
  52.         int node = S.top();
  53.         S.pop();
  54.         if(viz[node] == 1) {
  55.             ++cnt;
  56.             dfs2(node);
  57.         }
  58.     }
  59.     for(int i = 1; i <= N; ++i)
  60.         if(sz[ctc[i]] > 1 || sz[ctc[i + N]] > 1)
  61.             fout << '1';
  62.         else
  63.             fout << '0';
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment