結果
問題 | No.1843 Tree ANDistance |
ユーザー | Ayush Gangwani |
提出日時 | 2022-02-25 01:02:10 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 488 ms / 2,000 ms |
コード長 | 5,476 bytes |
コンパイル時間 | 3,312 ms |
コンパイル使用メモリ | 259,276 KB |
実行使用メモリ | 7,180 KB |
最終ジャッジ日時 | 2024-07-02 14:48:52 |
合計ジャッジ時間 | 14,818 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 405 ms
7,052 KB |
testcase_01 | AC | 407 ms
7,048 KB |
testcase_02 | AC | 403 ms
7,176 KB |
testcase_03 | AC | 390 ms
7,048 KB |
testcase_04 | AC | 403 ms
7,180 KB |
testcase_05 | AC | 385 ms
7,052 KB |
testcase_06 | AC | 401 ms
7,048 KB |
testcase_07 | AC | 389 ms
6,944 KB |
testcase_08 | AC | 394 ms
7,040 KB |
testcase_09 | AC | 381 ms
6,856 KB |
testcase_10 | AC | 366 ms
6,932 KB |
testcase_11 | AC | 403 ms
7,040 KB |
testcase_12 | AC | 358 ms
6,944 KB |
testcase_13 | AC | 369 ms
6,940 KB |
testcase_14 | AC | 14 ms
5,376 KB |
testcase_15 | AC | 15 ms
5,376 KB |
testcase_16 | AC | 7 ms
5,376 KB |
testcase_17 | AC | 7 ms
5,376 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 16 ms
5,376 KB |
testcase_20 | AC | 8 ms
5,376 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 458 ms
6,400 KB |
testcase_29 | AC | 314 ms
5,376 KB |
testcase_30 | AC | 312 ms
5,376 KB |
testcase_31 | AC | 488 ms
6,564 KB |
testcase_32 | AC | 457 ms
6,272 KB |
testcase_33 | AC | 156 ms
5,376 KB |
testcase_34 | AC | 362 ms
5,664 KB |
testcase_35 | AC | 1 ms
5,376 KB |
testcase_36 | AC | 2 ms
5,376 KB |
testcase_37 | AC | 2 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> //#include<ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define int long long #define ld long double #define get(T,args...) T args; read(args); #define init(a,T,n,s) vector<T> a((n)+1,s); cin>>a; #define putn(args...) { put(args); cout<<"\n"; } #define pute(args...) { put(args); cout<<endl; } #define putr(args...) { putn(args) return ;} #define rep(i,a,b) for(int i=(a); i<(b); i++) #define repr(i,a,b) for(int i=(a); i>(b); i--) #define logarr(arr,f,l) { auto start=arr.begin(), end=arr.begin(); advance(start,(f)); advance(end,(l)); for(auto it=start; it!=end; it++) cout<<*it<<" "; cout<<"\n"; } #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sort_by(span,p) sort(span, [&](const auto &l, const auto &r) { return p; }) #define f(u, args...) [&](auto &&u) { return args; } #define g(u, v, args...) [&](auto &&u, auto &&v) { return args; } #define uniq(x) x.erase(unique(all(x)),x.end()); #define sz(x) (int)(x).size() #define mid(l,r) std::midpoint((l),(r)) #define zov(v,z,c) ((c) ? (v) : (z)) #define pii pair<int,int> #define pb emplace_back #define pf push_front #define fr first #define sc second #define sumv(v,b,e) accumulate((v).begin()+b,(v).begin()+e,0LL) #define minv(v,b,e) min_element((v).begin()+b,(v).begin()+e) #define maxv(v,b,e) max_element((v).begin()+b,(v).begin()+e) #define precise(n) cout<<fixed<<setprecision((n)) #define bpc(n) std::popcount((unsigned long long)(n)) #define hsb(n) std::has_single_bit((unsigned long long)(n)) #define MSB(n) std::bit_floor((unsigned long long)(n)) #define ctz(n) ((n) ? __builtin_ctzll((unsigned long long)(n)) : 0) #define clz(n) ((n) ? __builtin_clzll((unsigned long long)(n)) : 64) #define LSB(n) ((n)&(-(n))) #define vec(args...) std::vector<args> #define set(args...) std::set<args,Comp<args> > #define pqueue(args...) std::priority_queue<args,vector<args>,Comp<args> > //#define oset(args...) tree<args, null_type, less<args>, rb_tree_tag, tree_order_statistics_node_update> const int N = 100001, M = 998244353, mod = 1000000007, MX = INT64_MAX, MN = INT64_MIN, oo = 1e18; #ifdef DEBUG #include "./Debug/debug.hpp" #else #define debug(args...) 0; #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #endif //using namespace __gnu_pbds; using namespace std; vec(string) tokenizer(string str,char ch) {std::istringstream var((str)); vec(string) v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;} template<typename T> struct Comp { bool operator()(const T& l, const T& r) const { return l < r; } }; template<typename T1, typename T2> inline istream& operator >> (istream& in, pair<T1,T2>& a) { in>>a.first>>a.second; return in; } template<typename T1, typename T2> inline ostream& operator << (ostream& out, pair<T1,T2> a) { out<<a.first<<" "<<a.second; return out; } template<typename T> istream& operator >> (istream& in, vector<T>& v) { rep(i,1,sz(v)) cin>>v[i]; return in; } template<typename T,typename T1> inline bool amax(T &a,T1 b){ if(b>a) { a=b; return true; } return false; } template<typename T,typename T1> inline bool amin(T &a,T1 b){ if(b<a) { a=b; return true; } return false; } void read(auto&... args) { ((cin>>args), ...); } void put(auto&&... args) { ((cout<<args<<" "), ...);} void file_i_o() { freopen("./tests/test01.txt", "r", stdin); freopen("./tests/output01.txt", "w", stdout); } class DSU { vector<int> par, sizes; public: DSU(int n) : par(n+1), sizes(n+1,1) { for(int i=1; i<=n; i++) par[i] = i; } int find_set(int x) { return par[x] = (par[x] == x) ? x : find_set(par[x]); } bool merge(int x, int y) { x = find_set(x); y = find_set(y); if(x == y) return false; if(sizes[y] > sizes[x]) swap(x,y); sizes[x] += sizes[y]; par[y] = x; return true; } int get_size(int x) { return sizes[find_set(x)]; } }; long long expm(long long a, long long b, long long m=mod) { long long res=1; while(b) { if(b & 1) { res=((res % m) * (a % m)) % m; } b/=2; a=((a % m) * (a % m)) % m; } return res; } void solve(int T) { get(int,n) vector<array<int,3>> edges(n-1); for(auto&[a,b,w]:edges) cin>>a>>b>>w; int ans = 0; rep(i,0,32) { DSU dsu(n); for(auto& [a,b,w]:edges) { if((w>>i)&1) { dsu.merge(a,b); } } rep(j,1,n+1) { if(dsu.find_set(j) == j) { int sz = dsu.get_size(j); int val = ((((sz*(sz-1))%mod) * expm(2,mod-2,mod))%mod); ans = (ans + (val*(1LL<<i))%mod)%mod; } } } putn(ans) } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //file_i_o(); int t=1; rep(i,1,t+1) { //cout<<"Case #"<<i<<": "; solve(t); } #ifndef ONLINE_JUDGE cerr<<"\ntime taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<"\n"; #endif return 0; }