06a6ad17f5 2020-09-10 kinaba: #include <iostream> 06a6ad17f5 2020-09-10 kinaba: #include <sstream> 06a6ad17f5 2020-09-10 kinaba: #include <iomanip> 06a6ad17f5 2020-09-10 kinaba: #include <vector> 06a6ad17f5 2020-09-10 kinaba: #include <string> 06a6ad17f5 2020-09-10 kinaba: #include <map> 06a6ad17f5 2020-09-10 kinaba: #include <set> 06a6ad17f5 2020-09-10 kinaba: #include <algorithm> 06a6ad17f5 2020-09-10 kinaba: #include <numeric> 06a6ad17f5 2020-09-10 kinaba: #include <iterator> 06a6ad17f5 2020-09-10 kinaba: #include <functional> 06a6ad17f5 2020-09-10 kinaba: #include <complex> 06a6ad17f5 2020-09-10 kinaba: #include <queue> 06a6ad17f5 2020-09-10 kinaba: #include <stack> 06a6ad17f5 2020-09-10 kinaba: #include <cmath> 06a6ad17f5 2020-09-10 kinaba: #include <cassert> 06a6ad17f5 2020-09-10 kinaba: #include <tuple> 06a6ad17f5 2020-09-10 kinaba: using namespace std; 06a6ad17f5 2020-09-10 kinaba: typedef long long LL; 06a6ad17f5 2020-09-10 kinaba: typedef complex<double> CMP; 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: struct UnionFind 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: vector<int> uf, sz; 06a6ad17f5 2020-09-10 kinaba: int nc; 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: UnionFind(int N) : uf(N), sz(N, 1), nc(N) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: for (int i = 0; i<N; ++i) uf[i] = i; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: int size() 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: return nc; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: int size(int a) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: return sz[Find(a)]; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: int Find(int a) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: return uf[a] == a ? a : uf[a] = Find(uf[a]); 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: // Semi-compression w.o. recursion. 06a6ad17f5 2020-09-10 kinaba: //{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; } 06a6ad17f5 2020-09-10 kinaba: bool Union(int a, int b) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: a = Find(a); 06a6ad17f5 2020-09-10 kinaba: b = Find(b); 06a6ad17f5 2020-09-10 kinaba: if (a != b) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: if (sz[a] >= sz[b]) swap(a, b); 06a6ad17f5 2020-09-10 kinaba: uf[a] = b; 06a6ad17f5 2020-09-10 kinaba: sz[b] += sz[a]; 06a6ad17f5 2020-09-10 kinaba: --nc; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: return (a != b); 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: }; 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: static const unsigned MODVAL = 1000000007; 06a6ad17f5 2020-09-10 kinaba: struct mint 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: unsigned val; 06a6ad17f5 2020-09-10 kinaba: mint() :val(0) {} 06a6ad17f5 2020-09-10 kinaba: mint(int x) :val(x%MODVAL) {} 06a6ad17f5 2020-09-10 kinaba: mint(unsigned x) :val(x%MODVAL) {} 06a6ad17f5 2020-09-10 kinaba: mint(LL x) :val(x%MODVAL) {} 06a6ad17f5 2020-09-10 kinaba: }; 06a6ad17f5 2020-09-10 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val + y.val; } 06a6ad17f5 2020-09-10 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; } 06a6ad17f5 2020-09-10 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 06a6ad17f5 2020-09-10 kinaba: mint operator+(mint x, mint y) { return x += y; } 06a6ad17f5 2020-09-10 kinaba: mint operator-(mint x, mint y) { return x -= y; } 06a6ad17f5 2020-09-10 kinaba: mint operator*(mint x, mint y) { return x *= y; } 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; } 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: class TheSocialNetwork { public: 06a6ad17f5 2020-09-10 kinaba: int minimumCut(int n, int m, vector <int> u, vector <int> v, vector <int> l) 06a6ad17f5 2020-09-10 kinaba: { 06a6ad17f5 2020-09-10 kinaba: vector<tuple<int, int, int>> e; 06a6ad17f5 2020-09-10 kinaba: for (int i = 0; i < m; ++i) 06a6ad17f5 2020-09-10 kinaba: e.emplace_back(l[i], u[i]-1, v[i]-1); 06a6ad17f5 2020-09-10 kinaba: sort(e.begin(), e.end()); 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: vector<bool> use(m, false); 06a6ad17f5 2020-09-10 kinaba: for (int k = e.size() - 1; k >= 0; --k) { 06a6ad17f5 2020-09-10 kinaba: use[k] = true; 06a6ad17f5 2020-09-10 kinaba: if (is_connected(n, m, use, e)) 06a6ad17f5 2020-09-10 kinaba: use[k] = false; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: mint ans = 0; 06a6ad17f5 2020-09-10 kinaba: for (int i = 0; i < m; ++i) if (!use[i]) 06a6ad17f5 2020-09-10 kinaba: ans += POW(2, get<0>(e[i])); 06a6ad17f5 2020-09-10 kinaba: return ans.val; 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: bool is_connected(int n, int m, const vector<bool>& use, const vector<tuple<int, int, int>>& e) { 06a6ad17f5 2020-09-10 kinaba: UnionFind uf(n); 06a6ad17f5 2020-09-10 kinaba: for (int i = 0; i < m; ++i) if (use[i]) 06a6ad17f5 2020-09-10 kinaba: uf.Union(get<1>(e[i]), get<2>(e[i])); 06a6ad17f5 2020-09-10 kinaba: return uf.size() == 1; 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: }; 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: // BEGIN CUT HERE 06a6ad17f5 2020-09-10 kinaba: #include <ctime> 06a6ad17f5 2020-09-10 kinaba: double start_time; string timer() 06a6ad17f5 2020-09-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 06a6ad17f5 2020-09-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 06a6ad17f5 2020-09-10 kinaba: { os << "{ "; 06a6ad17f5 2020-09-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 06a6ad17f5 2020-09-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 06a6ad17f5 2020-09-10 kinaba: void verify_case(const int& Expected, const int& Received) { 06a6ad17f5 2020-09-10 kinaba: bool ok = (Expected == Received); 06a6ad17f5 2020-09-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 06a6ad17f5 2020-09-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 06a6ad17f5 2020-09-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 06a6ad17f5 2020-09-10 kinaba: #define END verify_case(_, TheSocialNetwork().minimumCut(n, m, u, v, l));} 06a6ad17f5 2020-09-10 kinaba: int main(){ 06a6ad17f5 2020-09-10 kinaba: 06a6ad17f5 2020-09-10 kinaba: CASE(0) 06a6ad17f5 2020-09-10 kinaba: int n = 6; 06a6ad17f5 2020-09-10 kinaba: int m = 6; 06a6ad17f5 2020-09-10 kinaba: int u_[] = {1, 2, 3, 4, 5, 6} ; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = {2, 3, 4, 5, 6, 1}; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = {1, 7, 3, 4, 6, 12}; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = 10; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: CASE(1) 06a6ad17f5 2020-09-10 kinaba: int n = 5; 06a6ad17f5 2020-09-10 kinaba: int m = 7; 06a6ad17f5 2020-09-10 kinaba: int u_[] = {1, 1, 1, 2, 2, 3, 3}; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = {5, 3, 2, 5, 3, 5, 4}; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = {1, 8, 2, 3, 4, 6, 9}; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = 28; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: CASE(2) 06a6ad17f5 2020-09-10 kinaba: int n = 7; 06a6ad17f5 2020-09-10 kinaba: int m = 6; 06a6ad17f5 2020-09-10 kinaba: int u_[] = {1, 1, 2, 2, 3, 3}; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = {2, 3, 4, 5, 6, 7}; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = {7, 11, 6, 9, 20, 15}; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = 64; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: CASE(3) 06a6ad17f5 2020-09-10 kinaba: int n = 8; 06a6ad17f5 2020-09-10 kinaba: int m = 11; 06a6ad17f5 2020-09-10 kinaba: int u_[] = {1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 7}; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = {2, 8, 3, 5, 4, 6, 7, 5, 6, 8, 8}; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = {2, 3, 1, 6, 11, 8, 9, 10, 7, 4, 5}; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = 12; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: CASE(4) 06a6ad17f5 2020-09-10 kinaba: int n = 13; 06a6ad17f5 2020-09-10 kinaba: int m = 56; 06a6ad17f5 2020-09-10 kinaba: int u_[] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 11, 11, 12}; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = {3, 4, 5, 7, 9, 12, 13, 3, 5, 8, 9, 10, 12, 13, 5, 6, 8, 9, 10, 11, 12, 5, 6, 7, 9, 11, 13, 7, 8, 9, 11, 12, 7, 8, 9, 10, 13, 8, 9, 10, 11, 12, 13, 9, 11, 12, 10, 11, 12, 13, 11, 12, 13, 12, 13, 13}; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = {82, 240, 395, 1041, 1165, 1274, 1540, 1650, 1904, 2306, 2508, 3162, 3380, 3637, 3778, 3913, 3971, 4101, 4148, 4218, 4394, 4434, 5107, 6147, 6280, 6337, 6461, 6490, 7056, 8024, 8373, 8924, 8961, 9058, 9304, 9359, 10899, 11049, 11090, 11174, 11269, 11356, 11547, 11808, 12566, 12591, 13322, 13447, 13667, 13672, 15013, 15319, 16153, 16447, 16454, 16470}; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = 504663883; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: /* 06a6ad17f5 2020-09-10 kinaba: CASE(5) 06a6ad17f5 2020-09-10 kinaba: int n = ; 06a6ad17f5 2020-09-10 kinaba: int m = ; 06a6ad17f5 2020-09-10 kinaba: int u_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = ; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: CASE(6) 06a6ad17f5 2020-09-10 kinaba: int n = ; 06a6ad17f5 2020-09-10 kinaba: int m = ; 06a6ad17f5 2020-09-10 kinaba: int u_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> u(u_, u_+sizeof(u_)/sizeof(*u_)); 06a6ad17f5 2020-09-10 kinaba: int v_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 06a6ad17f5 2020-09-10 kinaba: int l_[] = ; 06a6ad17f5 2020-09-10 kinaba: vector <int> l(l_, l_+sizeof(l_)/sizeof(*l_)); 06a6ad17f5 2020-09-10 kinaba: int _ = ; 06a6ad17f5 2020-09-10 kinaba: END 06a6ad17f5 2020-09-10 kinaba: */ 06a6ad17f5 2020-09-10 kinaba: } 06a6ad17f5 2020-09-10 kinaba: // END CUT HERE