Topic: https://vjudge.net/contest/307753#problem/G
Meaning of the questions: a tree allows you to find the total number of paths and 3% of the number of paths
Ideas: dotted flood problem
#include <cstdio> #include <CString> #include <the cmath> #include <algorithm> #include <the iostream> #include <Vector> #include <Queue> #define MAXN 100005 #define MOD 0x3f3f3f3f the using namespace STD; typedef Long Long ll; ll DA; Vector <pair <ll, ll>> MP [MAXN], XX [MAXN]; // save enough FIG ll E [MAXN]; BOOL VIS [MAXN]; // tag have used gravity ll MAXSIZE [MAXN], DIS [MAXN], D [MAXN], in Flag [MAXN]; // MAXSIZE largest subtree of the current node SIZ LL [MAXN], xD [MAXN]; // DIS distance to the center of gravity d distance appeared LL n-, m, K, RT, SUM, QE, QE2, ANS1, ANS2; // SIZ current node subtree e rt appears from the number of current focus void find (X LL, LL F) { // find the center of gravity SIZ [X] = . 1 ; MAXSIZE [X] = 0 ; for ( int I = 0 ; I <MP [ X] .size (); I ++ ) { pair <LL, LL> Q = MP [X] [I]; IF (q.first == F || VIS [q.first]) Continue ; // VIS array tags We have used gravity Find (q.first, X); SIZ [X] + = SIZ [q.first]; MAXSIZE [X] = max (MAXSIZE [X], SIZ [q.first]); } MAXSIZE [X] = max (MAXSIZE [X], SUM-SIZ [X]); // total number of nodes minus the current sub the number of tree = the current node is the root of the father idea number tree IF (MAXSIZE [X] < MAXSIZE [RT]) { RT = X; } } void Query (LL Z, LL SM) { IF (Z> ANS1) { ANS1 = Z; ANS2 = SM; } the else IF (Z == ANS1) { ANS2 + = SM; } } void get_dis (X LL, F LL, LL len) { IF (len% . 3==0) ans1++; ans2++; ans1+=flag[(3-len%3+3)%3]; ans2+=flag[0]+flag[1]+flag[2]; e[len%3]++; for(int i=0;i<mp[x].size();i++){ pair<ll,ll> q=mp[x][i]; if(q.first==f||vis[q.first]) continue; //dis[q.first]=(dis[x]+len)%3; get_dis(q.first,x,(len+q.second)%3); } } void divide(ll x){ vis[x]=1; //printf("rt=%lld ans1=%lld ans2=%lld\n",x,ans1,ans2); for(int i=0;i<mp[x].size();i++){ pair<ll,ll> q=mp[x][i]; if(vis[q.first]) continue; //dis[x]=q.second; get_dis(q.first,x,q.second%3); for(int j=0;j<3;j++){ flag[j]+=e[j]; e[j]=0; } } for(int i=0;i<3;i++){ flag[i]=0; } for(int i=0;i<mp[x].size();i++){ pair<ll,ll> q=mp[x][i]; if(vis[q.first]) continue; //if(da>0) break; sum=siz[q.first]; rt=0; maxsize[rt]=mod; find(q.first,x); divide(rt); } // vis[x]=0; } void init(){ ans1=0;ans2=0; for(int i=0;i<=n;i++) mp[i].clear(); for(int i=0;i<=n;i++) vis[i]=0; for(int i=0;i<3;i++) flag[i]=0; } int main(){ ll t; while(scanf("%lld",&n)!=EOF){ ll a,b,c; init(); for(int i=1;i<=n-1;i++){ scanf("%lld%lld%lld",&a,&b,&c); mp[a].push_back(make_pair(b,c)); mp[b].push_back(make_pair(a,c)); } sum=n;//当前节点数 rt=0; maxsize[0]=mod;//置初值 find(1,0); divide(rt); ans1*=2; ans2*=2; ans1+=n; ans2+=n; ll w=__gcd(ans1,ans2); //printf("%lld/%lld\n",ans1,ans2); printf("%lld/%lld\n",ans1/w,ans2/w); } }