QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#90298 | #5208. Jumbled Trees | lijunyi | WA | 2ms | 3760kb | C++23 | 2.5kb | 2023-03-22 17:05:37 | 2023-03-22 17:05:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=5000;
int n,m,mod;
int tot=1,pre[maxn],son[maxn],now[maxn],id[maxn],w[maxn];
int dep[maxn],fa[maxn],upv[maxn],vis[maxn],kd[maxn];
int bs=-1,sum,Fa[maxn],siz[maxn],ss[maxn],ww[maxn];
vector<int> S,T,G[maxn];
vector<pair<int,vector<int> > > ans;
int find(int x){return x==Fa[x]?x:Fa[x]=find(Fa[x]);}
void put(int x,int y,int z)
{
pre[++tot]=now[x];
now[x]=tot;
son[tot]=y;
id[tot]=z;
}
void add(int p,int q,int v)
{
sum=(sum+v)%mod;
T.clear();T.push_back(q);
for(auto t:S)
if(t!=p)
T.push_back(t);
sort(T.begin(),T.end());
ans.emplace_back(v,T);
}
int power(int x,int y)
{
int ans=1;
while(y)
{
if(y&1)
ans=1ll*ans*x%mod;
y>>=1;
x=1ll*x*x%mod;
}
return ans;
}
void merge(int x,int y)
{
if(find(x)==find(y))
return ;
G[x].push_back(y),G[y].push_back(x);
x=find(x),y=find(y);
Fa[x]=y,siz[y]+=siz[x],(ss[y]+=ss[x])%=mod;
}
void dfs1(int x,int ff,int pp)
{
fa[x]=ff;dep[x]=dep[ff]+1;
for(int p=now[x];p;p=pre[p])
{
if(p==(pp^1))
continue;
int t=son[p];
if(dep[t])
{
if(dep[t]<dep[x])
{
kd[id[p]]=1;
int l=x;
while(l!=t)
vis[l]=1,merge(id[p],upv[l]),l=fa[l];
}
continue;
}
upv[t]=id[p];
S.push_back(id[p]);
dfs1(t,x,p);
if(!vis[t])
{
if(bs==-1)
bs=w[id[p]];
else if(bs!=w[id[p]])
{
puts("-1");
exit(0);
}
}
}
}
void dfs2(int x,int fa)
{
for(auto t:G[x])
if(t!=fa)
{
dfs2(t,x);
if(kd[x])
add(t,x,(bs-w[t]+mod)%mod),w[x]=(0ll+w[x]-bs+w[t]+mod)%mod,w[t]=bs;
else
add(x,t,w[t]),w[x]=(w[x]+w[t])%mod,w[t]=0;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&mod);
int s=0;
for(int i=1,u,v;i<=m;i++)
scanf("%d%d%d",&u,&v,&w[i]),put(u,v,i),put(v,u,i),s=(s+w[i])%mod,Fa[i]=i,ss[i]=w[i],siz[i]=1;
dfs1(1,0,0);
if(bs==-1)
for(int i=1;i<=m;i++)
if(i==Fa[i]&&(siz[i]-1)%mod!=0)
bs=1ll*ss[i]*power(siz[i]-1,mod-2)%mod;
if(bs==-1)
{
for(int i=1;i<=m;i++)
ww[i]=w[i];
for(int i=1;i<=m;i++)
if(i==Fa[i])
{
dfs2(i,0),bs=(mod-w[i])%mod;
break;
}
for(int i=1;i<=m;i++)
w[i]=ww[i];
}
for(int i=1;i<=m;i++)
if(i==Fa[i])
{
dfs2(i,0);
if(w[i]!=(1-kd[i])*bs)
return puts("-1"),0;
}
add(*S.begin(),*S.begin(),(bs-sum+mod)%mod);
printf("%d\n",(int)ans.size());
for(auto [x,y]:ans)
{
printf("%d ",x);
for(auto t:y)
printf("%d ",t);
puts("");
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3680kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
3 20 1 3 10 1 2 30 2 3
result:
ok Participant found an answer (3 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 2ms
memory: 3708kb
input:
2 2 37 1 2 8 1 2 15
output:
2 8 1 15 2
result:
ok Participant found an answer (2 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 2ms
memory: 3560kb
input:
5 4 5 1 3 1 2 3 2 2 5 3 4 1 4
output:
-1
result:
ok Both jury and participant did not find an answer
Test #4:
score: 0
Accepted
time: 2ms
memory: 3572kb
input:
10 15 997 4 3 459 9 7 94 9 8 767 10 2 877 5 8 258 3 4 166 8 5 621 8 10 619 9 1 316 10 5 516 3 10 125 1 7 961 3 6 500 4 10 976 3 4 842
output:
-1
result:
ok Both jury and participant did not find an answer
Test #5:
score: -100
Wrong Answer
time: 2ms
memory: 3760kb
input:
20 30 9973 1 10 696 3 8 2905 12 7 6609 20 10 1962 11 9 8430 19 2 412 6 3 6936 19 7 9113 14 15 5635 15 7 1770 13 10 3182 3 16 2625 17 1 7387 11 5 3700 9 15 1048 2 3 7717 12 10 8625 7 13 8141 5 14 2245 6 4 2819 18 19 8709 18 5 6191 17 10 7606 9 20 8626 17 4 8848 4 13 1073 10 8 2277 14 2 7714 11 8 5318...
output:
-1
result:
wrong answer Participant did not find an answer, while jury found (59 trees)