QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#398012#3758. 2019xlwangAC ✓556ms320468kbC++142.2kb2024-04-24 21:08:542024-04-24 21:08:54

Judging History

你现在查看的是最新测评结果

  • [2024-04-24 21:08:54]
  • 评测
  • 测评结果:AC
  • 用时:556ms
  • 内存:320468kb
  • [2024-04-24 21:08:54]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
using namespace std;
inline int read(){
	int x=0;
	bool f=0;
	char c=getchar();
	while(!isdigit(c)) f|=(c=='-'),c=getchar();
	while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
int n;
const int Maxn=2e4+10;
struct node{
    int nxt,to,d;
}e[Maxn<<1];
int head[Maxn],cnt;
inline void add(int u,int v,int w){
    ++cnt;
    e[cnt].nxt=head[u];
    head[u]=cnt;
    e[cnt].to=v;
    e[cnt].d=w;
}
int f[Maxn][2020];
inline void clear(){
    cnt=0;
    fr(i,1,n) head[i]=0;
    fr(i,1,n) fr(j,0,2018) f[i][j]=0;
}
int s[Maxn];
const int mod=2019;
int ans;
inline void dfs(int x,int fa){
    for(register int i=head[x];i;i=e[i].nxt){
        int y;y=e[i].to;
        if(y==fa) continue;
        s[y]=(s[x]+e[i].d)%mod;
        dfs(y,x);
    }
}
inline void dfs1(int x,int fa){
    f[x][s[x]]=1;
    for(register int i=head[x];i;i=e[i].nxt){
        int y;y=e[i].to;
        if(y==fa) continue;
        dfs1(y,x);
        fr(i,0,mod-1) ans+=f[x][i]*f[y][(mod-i+s[x]*2)%mod];
        fr(i,0,mod-1) f[x][i]+=f[y][i];
    }
}
inline void work(){
    clear();
    ans=0;
    fr(i,1,n-1){
        int x,y,z;
        x=read();y=read();z=read();
        add(x,y,z);add(y,x,z);
    }
    dfs(1,0);dfs1(1,0);
    // fr(i,1,n) cout<<i<<' '<<s[i]<<endl;
    writeln(ans);
}
inline void init(){
	while(cin>>n) work();
}
signed main(){
	// freopen("input.in","r",stdin);
	// freopen("output.out","w",stdout);
    init();
    // printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 556ms
memory: 320468kb

input:

500
393 136 1551
456 231 1348
286 161 1460
63 343 1265
319 427 834
429 181 1322
456 191 802
23 217 1621
117 292 1627
378 51 409
268 369 1447
348 381 1206
464 147 439
73 430 922
87 233 1521
33 235 19
11 462 351
311 449 7
451 26 1971
256 109 1686
145 14 1126
11 126 372
117 429 705
385 57 1418
147 220 ...

output:

73
60
57
68
56
63
43
60
68
60
65
70
67
51
45
50
53
62
55
63
61
51
54
63
56
58
74
69
71
65
70
62
61
73
59
84
49
62
63
70
60
55
52
60
50
52
53
64
62
54
61
72
49
48
59
65
63
67
63
63
57
80
67
56
43
61
54
67
53
65
68
58
56
59
61
66
57
58
55
68
99708
98538
199990000

result:

ok 83 numbers