#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } // #define int long long using ull = unsigned long long; const int nax = 5e5 + 5; vector<pair<int,int>> edges[nax]; // (b, edge_id) int depth[nax], count_ups[nax]; ull his_hash[nax]; pair<int,int> in[nax]; pair<int,int> parent[nax]; // (b, edge_id) bool in_tree[nax]; ull R() { return (ull) RAND_MAX * (ull) rand() + rand(); } // void add_self(pair<int, ull>& a, const pair<int, ull>& b) { // a.first += b.first; // a.second += b.second; // } // void sub_self(pair<int, ull>& a, const pair<int, ull>& b) { // a.first -= b.first; // a.second -= b.second; // } struct Up { ull hash = 0; multiset<pair<int,int>> ds; // depth, edge_id void consider(pair<int,int> p) { hash ^= his_hash[p.second]; // add_self(hash, his_hash[p.second]); ds.insert(p); } void trim_to(int d) { while(!ds.empty()) { auto it = prev(ds.end()); if(it->first < d) { break; } hash ^= his_hash[it->second]; // sub_self(hash, his_hash[it->second]); ds.erase(it); } } }; Up candidates[nax]; vector<int> path{-1}; void dfs(int a) { assert(depth[a] == (int) path.size()); path.push_back(a); Up& ups = candidates[a]; for(pair<int,int> e : edges[a]) { int b = e.first; int edge_id = e.second; if(edge_id == parent[a].second) { continue; } if(depth[b] == 0) { parent[b] = {a, edge_id}; in_tree[edge_id] = true; debug() << imie(a) << " -> " << imie(b); depth[b] = depth[a] + 1; dfs(b); Up& his = candidates[b]; his_hash[edge_id] = his.hash; if(his.ds.size() > ups.ds.size()) { swap(ups.hash, his.hash); ups.ds.swap(his.ds); } for(pair<int,int> p : his.ds) { // TODO, smaller to greater (log sorted vectors will be log instead of log^2?) if(p.first < depth[a]) { ups.consider(p); } } ups.trim_to(depth[a]); } else if(depth[b] < depth[a]) { // edge up // his_hash[edge_id].first = 1; his_hash[edge_id] = R(); ups.consider(make_pair(depth[b], edge_id)); } } path.pop_back(); // return ups; } // #undef int int main() { // #define int long long int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); edges[a].emplace_back(b, i); edges[b].emplace_back(a, i); in[i] = {a, b}; } depth[1] = 1; parent[1] = {-1, -1}; dfs(1); // long long answer = 0; // debug() << imie(candidates); map<ull, int> freq; int bridges = 0; for(int i = 0; i < m; ++i) { if(his_hash[i] == 0) { ++bridges; } else { ++freq[his_hash[i]]; } } long long answer = 0; auto two = [&](long long x) { return x * (x - 1) / 2; }; auto three = [&](long long x) { return x * (x - 1) * (x - 2) / 6; }; for(int i = 0; i < bridges; ++i) { answer += two(m - i - 1); } m -= bridges; debug() << imie(bridges); vector<pair<ull, int>> w(freq.begin(), freq.end()); debug() << imie(w); for(auto pp : w) { answer += three(pp.second); answer += two(pp.second) * (m - pp.second); } for(int i = 0; i < (int) w.size(); ++i) { // if(i % 1000 == 0) { // cerr << i << " "; // } for(int j = i + 1; j < (int) w.size(); ++j) { ull need = w[i].first ^ w[j].first; if(need < w[i].first) { int z = lower_bound(w.begin(), w.begin() + i, make_pair(need, 0)) - w.begin(); if(z < i && w[z].first == need) { answer += (long long) w[i].second * w[j].second * w[z].second; } } } } printf("%lld\n", answer); return 0; for(int i = 0; i < m; ++i) { debug() << imie(i) imie(his_hash[i]); for(int j = i + 1; j < m; ++j) { for(int k = j + 1; k < m; ++k) { ull a = his_hash[i]; ull b = his_hash[j]; ull c = his_hash[k]; if(a == 0 || b == 0 || c == 0 || a == b || a == c || b == c) { answer++; continue; } // if(in_tree[i] && in_tree[j] && in_tree[k]) { // if(a + b == c || a + c == b || b + c == a) { // debug() << "skipped " imie(i) imie(j) imie(k) imie(a) imie(b) imie(c); // } // continue; // } if((a ^ b ^ c) == 0) { // if(a + b == c || a + c == b || b + c == a) { answer++; } // bool ok = false; // for(int mask = 1; mask < 8; ++mask) { // ull total = 0; // for(int z = 0; z < 3; ++z) { // if(mask & (1 << z)) { // total ^= his_hash[vector<int>{i,j,k}[z]]; // } // } // if(total == 0) { // ok = true; // } // } // if(ok) { // answer++; // } } } } printf("%lld\n", answer); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } // #define int long long using ull = unsigned long long; const int nax = 5e5 + 5; vector<pair<int,int>> edges[nax]; // (b, edge_id) int depth[nax], count_ups[nax]; ull his_hash[nax]; pair<int,int> in[nax]; pair<int,int> parent[nax]; // (b, edge_id) bool in_tree[nax]; ull R() { return (ull) RAND_MAX * (ull) rand() + rand(); } // void add_self(pair<int, ull>& a, const pair<int, ull>& b) { // a.first += b.first; // a.second += b.second; // } // void sub_self(pair<int, ull>& a, const pair<int, ull>& b) { // a.first -= b.first; // a.second -= b.second; // } struct Up { ull hash = 0; multiset<pair<int,int>> ds; // depth, edge_id void consider(pair<int,int> p) { hash ^= his_hash[p.second]; // add_self(hash, his_hash[p.second]); ds.insert(p); } void trim_to(int d) { while(!ds.empty()) { auto it = prev(ds.end()); if(it->first < d) { break; } hash ^= his_hash[it->second]; // sub_self(hash, his_hash[it->second]); ds.erase(it); } } }; Up candidates[nax]; vector<int> path{-1}; void dfs(int a) { assert(depth[a] == (int) path.size()); path.push_back(a); Up& ups = candidates[a]; for(pair<int,int> e : edges[a]) { int b = e.first; int edge_id = e.second; if(edge_id == parent[a].second) { continue; } if(depth[b] == 0) { parent[b] = {a, edge_id}; in_tree[edge_id] = true; debug() << imie(a) << " -> " << imie(b); depth[b] = depth[a] + 1; dfs(b); Up& his = candidates[b]; his_hash[edge_id] = his.hash; if(his.ds.size() > ups.ds.size()) { swap(ups.hash, his.hash); ups.ds.swap(his.ds); } for(pair<int,int> p : his.ds) { // TODO, smaller to greater (log sorted vectors will be log instead of log^2?) if(p.first < depth[a]) { ups.consider(p); } } ups.trim_to(depth[a]); } else if(depth[b] < depth[a]) { // edge up // his_hash[edge_id].first = 1; his_hash[edge_id] = R(); ups.consider(make_pair(depth[b], edge_id)); } } path.pop_back(); // return ups; } // #undef int int main() { // #define int long long int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); edges[a].emplace_back(b, i); edges[b].emplace_back(a, i); in[i] = {a, b}; } depth[1] = 1; parent[1] = {-1, -1}; dfs(1); // long long answer = 0; // debug() << imie(candidates); map<ull, int> freq; int bridges = 0; for(int i = 0; i < m; ++i) { if(his_hash[i] == 0) { ++bridges; } else { ++freq[his_hash[i]]; } } long long answer = 0; auto two = [&](long long x) { return x * (x - 1) / 2; }; auto three = [&](long long x) { return x * (x - 1) * (x - 2) / 6; }; for(int i = 0; i < bridges; ++i) { answer += two(m - i - 1); } m -= bridges; debug() << imie(bridges); vector<pair<ull, int>> w(freq.begin(), freq.end()); debug() << imie(w); for(auto pp : w) { answer += three(pp.second); answer += two(pp.second) * (m - pp.second); } for(int i = 0; i < (int) w.size(); ++i) { // if(i % 1000 == 0) { // cerr << i << " "; // } for(int j = i + 1; j < (int) w.size(); ++j) { ull need = w[i].first ^ w[j].first; if(need < w[i].first) { int z = lower_bound(w.begin(), w.begin() + i, make_pair(need, 0)) - w.begin(); if(z < i && w[z].first == need) { answer += (long long) w[i].second * w[j].second * w[z].second; } } } } printf("%lld\n", answer); return 0; for(int i = 0; i < m; ++i) { debug() << imie(i) imie(his_hash[i]); for(int j = i + 1; j < m; ++j) { for(int k = j + 1; k < m; ++k) { ull a = his_hash[i]; ull b = his_hash[j]; ull c = his_hash[k]; if(a == 0 || b == 0 || c == 0 || a == b || a == c || b == c) { answer++; continue; } // if(in_tree[i] && in_tree[j] && in_tree[k]) { // if(a + b == c || a + c == b || b + c == a) { // debug() << "skipped " imie(i) imie(j) imie(k) imie(a) imie(b) imie(c); // } // continue; // } if((a ^ b ^ c) == 0) { // if(a + b == c || a + c == b || b + c == a) { answer++; } // bool ok = false; // for(int mask = 1; mask < 8; ++mask) { // ull total = 0; // for(int z = 0; z < 3; ++z) { // if(mask & (1 << z)) { // total ^= his_hash[vector<int>{i,j,k}[z]]; // } // } // if(total == 0) { // ok = true; // } // } // if(ok) { // answer++; // } } } } printf("%lld\n", answer); } |