Advertisement
999ms

Triangles

Dec 23rd, 2021
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define all(x) begin(x), end(x)
  4.  
  5. using namespace std;
  6. using ll = long long;
  7. const int N = 3003000;
  8. vector<pair<int, int>> g[N];
  9. vector<int> indG[N];
  10.  
  11. vector<pair<int, int>> edges;
  12. int GlobalCnt[N];
  13.  
  14. void inc(int x, int y, int z) {
  15.     // do something with triangles
  16.     GlobalCnt[x]++;
  17.     GlobalCnt[y]++;
  18.     GlobalCnt[z]++;
  19. }
  20.  
  21. int cmp2(pair<int,int> aa, pair<int,int> ba) {
  22.     int a = aa.first;
  23.     int b = ba.first;
  24.     return g[a].size() < g[b].size() || (g[a].size() == g[b].size() && a < b);
  25. }
  26.  
  27. int cmp(int a, int b) {
  28.     return g[a].size() < g[b].size() || (g[a].size() == g[b].size() && a < b);
  29. }
  30.  
  31.  
  32. void solve() {
  33.     int n, m;
  34.     cin >> n >> m;
  35.     for (int i = 0; i < m; i++) {
  36.         int x, y;
  37.         cin >> x >> y;
  38.         x--, y--;
  39.         int index = (int) edges.size();
  40.         g[x].emplace_back(y, index);
  41.         g[y].emplace_back(x, index);
  42.         edges.emplace_back(x, y);
  43.     }
  44.  
  45.     vector<tuple<int, int, int>> triples;
  46.     vector<int> p(n);
  47.     iota(p.begin(), p.end(), 0);
  48.     sort(p.begin(), p.end(), cmp);
  49.  
  50.     for (int v = 0; v < n; ++v)
  51.         sort(g[v].begin(), g[v].end(), cmp2);
  52.     vector<int> cnt(n, -1);
  53.     vector<int> intCnt(n, -1);
  54.  
  55.  
  56.     for (int v: p) {
  57.         for (auto[w, wInd]: g[v]) {
  58.             cnt[w] = v;
  59.             intCnt[w] = wInd;
  60.         }
  61.  
  62.         for (auto[u, uInd]: g[v]) {
  63.             if (cmp(v, u))
  64.                 break;
  65.  
  66.             for (auto[w, wInd]: g[u]) {
  67.                 if (cmp(u, w))
  68.                     break;
  69.                 if (cnt[w] >= 0) {
  70.                     inc(wInd, uInd, intCnt[w]);
  71.                 }
  72.             }
  73.         }
  74.         for (auto [w, wInd]: g[v]) {
  75.             cnt[w] = -1;
  76.         }
  77.     }
  78.  
  79.  
  80.     int pos = int(max_element(GlobalCnt, GlobalCnt + m) - GlobalCnt);
  81.     cout << GlobalCnt[pos] << endl;
  82.     auto[x, y] = edges[pos];
  83.     cout << x + 1 << ' ' << y + 1 << endl;
  84. }
  85.  
  86. int main() {
  87.     ios_base::sync_with_stdio(false);
  88.     cin.tie(nullptr);
  89.     cout.tie(nullptr);
  90.     solve();
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement