Dang_Quan_10_Tin

ROAD

Nov 27th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <map>
  5. #include <queue>
  6. #include <unordered_map>
  7. #define task "ROAD"
  8. using namespace std;
  9. using ll = long long;
  10. using ld = long double;
  11. using ull = unsigned long long;
  12.  
  13. const int N = 2e5 + 2;
  14. int n, m;
  15. int u[N], v[N], c[N];
  16. vector<int> adj[N], ans;
  17. unordered_map<int, pair<int, int>> par[N];
  18.  
  19.  
  20. void Read()
  21. {
  22.     cin >> n >> m;
  23.     for(int i = 1; i <= m; ++i)
  24.     {
  25.         cin >> u[i] >> v[i] >> c[i];
  26.         adj[u[i]].push_back(i);
  27.     }
  28. }
  29.  
  30. int BFS(int x)
  31. {
  32.     struct Tque
  33.     {
  34.         int col, v;
  35.         Tque(int v = 0, int col = 0)
  36.         {
  37.             this->v = v;
  38.             this->col = col;
  39.         }
  40.     };
  41.     queue<Tque> s;
  42.     for(auto i : adj[x])
  43.         if(par[v[i]].empty() ||
  44.                 (par[v[i]].size() == 1 && c[par[v[i]].begin()->first] != c[i]))
  45.         {
  46.             s.emplace(i, c[i]);
  47.             par[v[i]][i] = {x, 0};
  48.         }
  49.     while(s.size())
  50.     {
  51.         Tque top = s.front();
  52.         s.pop();
  53.         for(auto i : adj[v[top.v]])
  54.             if(c[i] != c[top.v])
  55.             {
  56.                 if(v[i] != x)
  57.                 {
  58.                     if((par[v[i]].empty() ||
  59.                             (par[v[i]].size() == 1 && c[par[v[i]].begin()->first] != c[i])))
  60.                     {
  61.                         s.emplace(i, top.col);
  62.                         par[v[i]][i] = {v[top.v], top.v};
  63.                     }
  64.                 }
  65.                 else
  66.                 {
  67.                     if(c[i] != top.col)
  68.                     {
  69.                         par[x][i] = {v[top.v], top.v};
  70.                         return i;
  71.                     }
  72.                 }
  73.             }
  74.     }
  75.     return -1;
  76. }
  77.  
  78. void Trace(int v, int j, const int &x, const int &vv)
  79. {
  80.     if(v == vv && j != x)
  81.         return;
  82.     Trace(par[v][j].first, par[v][j].second, x, vv);
  83.     ans.push_back(j);
  84. }
  85.  
  86. void Sub2()
  87. {
  88.     for(int i = 1; i <= n; ++i)
  89.     {
  90.         int v = BFS(i);
  91.         if(v != -1)
  92.         {
  93.             Trace(i, v, v, i);
  94.             cout << ans.size() << "\n";
  95.             for(auto j : ans)
  96.                 cout << j << " ";
  97.             return;
  98.         }
  99.         for(int i = 1; i <= n; ++i)
  100.             par[i].clear();
  101.     }
  102.     cout << -1;
  103. }
  104.  
  105. void Sub3()
  106. {
  107.     for(int i = 1; i <= n; ++i)
  108.     {
  109.         int v = BFS(i);
  110.         if(v != -1)
  111.         {
  112.             Trace(i, v, v, i);
  113.             cout << ans.size() << "\n";
  114.             for(auto j : ans)
  115.                 cout << j << " ";
  116.             return;
  117.         }
  118.         for(int i = 1; i <= n; ++i)
  119.             par[i].clear();
  120.     }
  121.     cout << -1;
  122. }
  123.  
  124. int32_t main()
  125. {
  126.     ios_base::sync_with_stdio(0);
  127.     cin.tie(0);
  128.     cout.tie(0);
  129.     if(fopen(task".INP", "r"))
  130.     {
  131.         freopen(task".INP", "r", stdin);
  132.         freopen(task".OUT", "w", stdout);
  133.     }
  134.     Read();
  135.     if(m <= 5000)
  136.         Sub2();
  137.     else
  138.         Sub3();
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment