QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#90238 | #5208. Jumbled Trees | lijunyi | WA | 0ms | 3708kb | C++23 | 2.1kb | 2023-03-22 15:25:10 | 2023-03-22 15:25:14 |
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],ok[maxn],kd[maxn];
int bs,sum,Fa[maxn],sz=0,ss=0;
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);
Fa[find(x)]=find(y);
}
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]&&bs!=w[id[p]])
{
puts("-1");
exit(0);
}
}
}
void dfs2(int x,int fa)
{
sum=(sum+w[x])%mod;
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;
}
kd[x]=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;
bs=1ll*s*power(n-1,mod-2)%mod;
dfs1(1,0,0);int ok=(w[11]==296);
for(int i=1;i<=m;i++)
if(kd[i])
{
dfs2(i,0);
if(ok)
cout<<bs<<" "<<w[i]<<" "<<sum<<endl;
if(w[i])
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3708kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
3 20 1 3 10 1 2 11 2 3
result:
wrong answer Wrong value of edge 1 (1-based): expected = 40, actual = 21