mr_dot_convict

160D-Edges_in_MST-Codeforces-mr.convict

Oct 30th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define x         first
  24. #define y         second
  25. #define fr(i,x,y) for (int i = int(x); i <= int(y); ++i)
  26. #define fR(i,x,y) for (int i = int(x); i >= int(y); --i)
  27. #define cnt(x)    __builtin_popcount(x)
  28. #define cntll(x)  __builtin_popcountll(x)
  29. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  30. #define un(x)     sort(x.begin(), x.end()), \
  31.   x.erase(unique(x.begin(), x.end()), x.end())
  32. using   ll  =     long long;
  33. using   ull =     unsigned long long;
  34. using   ff  =     long double;
  35. using   pii =     pair<int,int>;
  36. using   pil =     pair<int,ll>;
  37. using   vi  =     vector <int>;
  38. using   vvi =     vector <vi>;
  39. using   vp  =     vector <pii>;
  40. using   vvp =     vector <vp>;
  41. using   vl  =     vector<ll>;
  42. typedef tree
  43. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  44. ordered_set;
  45.  
  46. struct chash {
  47.   int operator () (pii x) const { return x.x*31 + x.y; }
  48. };
  49. gp_hash_table <pii, int, chash> mp;
  50.  
  51. #if __cplusplus > 201103L
  52. seed_seq seq{
  53.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  54.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  55.     (uint64_t) __builtin_ia32_rdtsc(),
  56.     (uint64_t) (uintptr_t) make_unique<char>().get()
  57. };
  58. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  59. #else
  60. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  61. mt19937 rng(seed);
  62. #endif
  63.  
  64. #define debug(args...) { \
  65.   /* WARNING : do NOT compile this debug func calls with following flags: // \
  66.    * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  67.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  68.   stringstream _ss(_s); \
  69.   istream_iterator<string> _it(_ss); err(_it, args); \
  70. }
  71. void err(istream_iterator<string> it) {
  72.   it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  73. }
  74. template<typename T, typename... Args>
  75. void err(istream_iterator<string> it, T a, Args... args) {
  76.   cerr << fixed << setprecision(15)
  77.     << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  78.   err(++it, args...);
  79. }
  80. /*}}}*/
  81. /*****************************************************************************/
  82. //Don’t practice until you get it right. Practice until you can’t get it wrong
  83.  
  84. signed main() {
  85.   IOS; PREC;
  86.   int n, m;
  87.   cin >> n >> m;
  88.  
  89.   struct Edge {
  90.     int u, v, w, id;
  91.     bool operator < (const Edge &o) const {
  92.       return w < o.w;
  93.     }
  94.   };
  95.   vector <Edge> edges;
  96.  
  97.   fr(e, 1, m) {
  98.     int u, v, w;
  99.     cin >> u >> v >> w;
  100.     --u, --v;
  101.     edges.push_back({u, v, w, e-1});
  102.   }
  103.   sort (edges.begin(), edges.end());
  104.  
  105.   vector <vector <Edge>> layers;
  106.   vector <Edge> t;
  107.  
  108.   t.push_back(edges[0]);
  109.   fr(i, 1, m-1) {
  110.     while (i <= m-1 && edges[i].w == t.back().w) {
  111.       t.push_back(edges[i]);
  112.       ++i;
  113.     }
  114.     layers.push_back(t);
  115.     t.clear();
  116.     if (i < m) t.push_back(edges[i]);
  117.   }
  118.   if (t.size()) layers.push_back(t);
  119.  
  120.   /*
  121.      for (auto l : layers) if (l.size()) {
  122.      for (Edge e : l) debug(e.u, e.v, e.w, e.id);
  123.      cerr << endl;
  124.      }
  125.      */
  126.  
  127.   vi rep(n), rnk(n);
  128.   fr(i, 0, n-1) rep[i] = i, rnk[i] = 1;
  129.  
  130.   function <int(int)> find_set = [&] (int x) -> int {
  131.     return x == rep[x] ? x : (rep[x] = find_set(rep[x]));
  132.   };
  133.  
  134.   auto merge = [&] (int u, int v) {
  135.     u = find_set(u), v = find_set(v);
  136.     if (rnk[v] > rnk[u]) rep[u] = v;
  137.     else rep[v] = u;
  138.     if (rnk[v] == rnk[u]) ++rnk[u];
  139.   };
  140.  
  141.   vector <string> ans(m, "yet");
  142.  
  143.   vi t_in(n), f(n), vis(n, false);
  144.   int timer = 0;
  145.   vvp G(n, vp());
  146.  
  147.   function <void(int, int)> dfs = [&] (int u, int last_id) -> void {
  148.     // debug(u, last_id);
  149.     vis[u] = true;
  150.     f[u] = t_in[u] = timer++;
  151.  
  152.     for (auto vpair : G[u]) {
  153.       int v = vpair.x, id = vpair.y;
  154.       if (id == last_id) continue;
  155.       if (vis[v]) f[u] = min(f[u], t_in[v]);
  156.       else {
  157.         dfs(v, id);
  158.         f[u] = min(f[u], f[v]);
  159.         if (f[v] > t_in[u]) { // this is a bridge edge
  160.           // debug("marking bridge", u, v, t_in[u], t_in[v], f[u], f[v]);
  161.           ans[id] = "any";
  162.         }
  163.       }
  164.       // debug(u, v, t_in[u], t_in[v], f[u], f[v]);
  165.     }
  166.   };
  167.  
  168.   set <int> vertices;
  169.   vector <Edge> take;
  170.   for (auto l : layers) if (l.size()) {
  171.  
  172.     /*
  173.     for (int i = 0; i < n; ++i) {
  174.       debug(i, find_set(rep[i]));
  175.     }
  176.     cerr << endl;
  177.     */
  178.     vertices.clear();
  179.     take.clear();
  180.  
  181.     for (Edge &e : l) {
  182.       e.u = find_set(e.u), e.v = find_set(e.v);
  183.       int u = e.u, v = e.v, id = e.id;
  184.       vertices.insert(u), vertices.insert(v);
  185.       G[u].clear(), G[v].clear();
  186.       if (u == v) ans[id] = "none";
  187.       else {
  188.         ans[id] = "at least one";
  189.         take.push_back(e);
  190.       }
  191.     }
  192.  
  193.     for (Edge e : take) {
  194.       int u = e.u, v = e.v, id = e.id;
  195.       // debug(u, v, w, id);
  196.       G[u].push_back(make_pair(v, id));
  197.       G[v].push_back(make_pair(u, id));
  198.       merge(u, v);
  199.     }
  200.  
  201.     for (int v : vertices) vis[v] = false;
  202.     for (int v : vertices) if (!vis[v]) dfs(v, -1);
  203.   }
  204.  
  205.   fr(i, 0, m-1) cout << ans[i] << '\n';
  206.  
  207.   return EXIT_SUCCESS;
  208. }
Add Comment
Please, Sign In to add comment