Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5 + 7, mod = 1e9 + 7;
  5. int n, m, ans = 1, dsu[N], sz[N];
  6. map<int, vector<pair<int, int>>> edges;
  7.  
  8. int get(int v) {
  9.   if (dsu[v] == v) {
  10.     return v;
  11.   }
  12.   return dsu[v] = get(dsu[v]);
  13. }
  14.  
  15. void merge(int a, int b) {
  16.   if (a == -1 || b == -1) {
  17.     return;
  18.   }
  19.   a = get(a);
  20.   b = get(b);
  21.   if (a == b) {
  22.     return;
  23.   }
  24.   if (sz[a] < sz[b]) {
  25.     swap(a, b);
  26.   }
  27.   dsu[b] = a;
  28.   sz[a] += sz[a] == sz[b];
  29. }
  30.  
  31. bool eq(pair<int, int> a, pair<int, int> b) {
  32.   return (get(a.first) == get(b.first) && get(a.second) == get(b.second)) || (get(a.first) == get(b.second) && get(a.second) == get(b.first));
  33. }
  34.  
  35. int main() {
  36.   ios::sync_with_stdio(0);
  37.   cin.tie(0); cout.tie(0);
  38.  
  39.   cin >> n >> m;
  40.   for (int i = 0; i < m; ++i) {
  41.     int a, b, c;
  42.     cin >> a >> b >> c;
  43.     a--; b--;
  44.     edges[c].emplace_back(a, b);
  45.   }
  46.   iota(dsu, dsu + n, 0);
  47.   for (auto i : edges) {
  48.     pair<int, int> a = { -1, -1 }, b = { -1, -1 }, c = { -1, -1 };
  49.     if (i.second.size() >= 1) {
  50.       a = i.second[0];
  51.       if (get(a.first) == get(a.second)) {
  52.         a = { -1, -1 };
  53.       }
  54.     }
  55.     if (i.second.size() >= 2) {
  56.       b = i.second[1];
  57.       if (get(b.first) == get(b.second)) {
  58.         b = { -1, -1 };
  59.       }
  60.     }
  61.     if (i.second.size() >= 3) {
  62.       c = i.second[2];
  63.       if (get(c.first) == get(c.second)) {
  64.         c = { -1, -1 };
  65.       }
  66.     }
  67.     int cnt = (a.first != -1) + (b.first != -1) + (c.first != -1);
  68.     if (cnt == 3) {
  69.       set<int> LOL;
  70.       LOL.insert(get(a.first));
  71.       LOL.insert(get(a.second));
  72.       LOL.insert(get(b.first));
  73.       LOL.insert(get(b.second));
  74.       LOL.insert(get(c.first));
  75.       LOL.insert(get(c.second));
  76.       if (eq(a, b) && eq(a, c)) {
  77.         ans = (1ll * ans * 3) % mod;
  78.       } else if (eq(a, b) || eq(a, c) || eq(b, c)) {
  79.         ans = (1ll * ans * 2) % mod;
  80.       } else if ((int) LOL.size() == 3) {
  81.         ans = (1ll * ans * 3) % mod;
  82.       }
  83.     } else if (cnt == 2) {
  84.       if (a.first == -1) {
  85.         swap(a, c);
  86.       } else if (b.first == -1) {
  87.         swap(b, c);
  88.       }
  89.       if (eq(a, b)) {
  90.         ans = (1ll * ans * 2) % mod;
  91.       }
  92.     }
  93.     merge(a.first, a.second);
  94.     merge(b.first, b.second);
  95.     merge(c.first, c.second);
  96.   }
  97.   bool flag = true;
  98.   for (int i = 0; i < n; ++i) {
  99.     flag &= get(i) == get(0);
  100.   }
  101.   if (flag) {
  102.     cout << ans << "\n";
  103.   } else {
  104.     cout << 0 << "\n";
  105.   }
  106.  
  107.   return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement