QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#398001 | #3758. 2019 | Graphcity | AC ✓ | 402ms | 162980kb | C++20 | 889b | 2024-04-24 21:05:19 | 2024-04-24 21:05:20 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=2e4,P=2019;
int n,f[Maxn+5][P+5]; ll ans;
vector<pair<int,int>> v[Maxn+5];
inline void dfs(int x,int fa,int d)
{
f[x][d]++; for(auto [y,z]:v[x]) if(y!=fa) dfs(y,x,(d+z)%P);
for(auto [y,z]:v[x]) if(y!=fa)
{
For(a,0,P-1) if(f[x][a]) ans+=1ll*f[x][a]*f[y][(P+d*2-a)%P];
For(a,0,P-1) f[x][a]+=f[y][a];
}
}
inline void Solve()
{
For(i,1,n) vector<pair<int,int>>().swap(v[i]),memset(f[i],0,sizeof(f[i]));
For(i,1,n-1)
{
int a,b,c; cin>>a>>b>>c;
v[a].emplace_back(b,c),v[b].emplace_back(a,c);
} dfs(1,0,0); cout<<ans<<endl; ans=0;
}
int main()
{
// freopen("1.in","r",stdin);
while(cin>>n) Solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 402ms
memory: 162980kb
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