QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#68676 | #5208. Jumbled Trees | chenshi | WA | 1ms | 1736kb | C++14 | 1.8kb | 2022-12-18 15:19:11 | 2022-12-18 15:19:12 |
Judging History
answer
#include<cstdio>
using namespace std;
const int o=2010;
int n,m,p,h[o],cnt,w[o],s,fa[o],ans,Ans[o][o],tot,d[o],uu,vv,st[o],tp,St[o],Tp,ban;bool vis[o];
inline int qp(int b,int f){int res=1;for(;f;f>>=1,b=b*1ll*b%p) if(f&1) res=res*1ll*b%p;return res;}
struct Edge{int v,p,id;}e[o];
inline void ad(int U,int V,int ID){e[++cnt].v=V;e[cnt].p=h[U];e[h[U]=cnt].id=ID;}
void Dfs(int nw){
vis[nw]=1;
for(int i=h[nw];i;i=e[i].p) if(!vis[e[i].v])
Ans[1][++tot]=fa[e[i].v]=e[i].id,w[e[i].id]=(w[e[i].id]+p-s)%p,d[e[i].v]=d[nw]+1,Dfs(e[i].v);
}
void ddfs(int nw){
for(int i=h[nw];i;i=e[i].p) if(e[i].id-ban&&!vis[e[i].v]) vis[e[i].v]=1,Ans[ans][++tot]=e[i].id,ddfs(e[i].v);
}
inline void slv(int x,int y,int w){
Ans[++ans][0]=w;tot=0;
for(int i=1;i<=n;++i) vis[i]=0;
for(int i=1;i<=Tp;++i) vis[e[St[i]*2-1].v]=1,vis[e[St[i]*2].v]=1;
for(int i=1;i<=Tp;++i) if(St[i]^y) Ans[ans][++tot]=St[i];
ban=y;
for(int i=1;i<=n;++i) if(vis[i]) ddfs(i);
}
void dfs(int nw){
st[++tp]=nw;
for(int i=h[nw];i;i=e[i].p) if(e[i].id^fa[nw]){
if(fa[e[i].v]==e[i].id){
dfs(e[i].v);
if(uu==nw) uu=0,vv=0;
}
if(d[e[i].v]<d[nw]&&(!uu||d[e[i].v]*2+1<d[uu]+d[vv])){
uu=st[d[e[i].v]];vv=st[d[e[i].v]+1];St[Tp=1]=e[i].id;
for(int j=d[e[i].v]+1;j<=d[nw];++j) St[++Tp]=fa[st[j]];
}
if(w[e[i].id]&&uu)
slv(e[i].id,fa[vv],w[e[i].id]),slv(fa[vv],e[i].id,p-w[e[i].id]),
w[fa[vv]]=(w[fa[vv]]+w[e[i].id])%p,w[e[i].id]=0;
}
--tp;
}
int main(){
scanf("%d%d%d",&n,&m,&p);
for(int i=1,u,v;i<=m;++i) scanf("%d%d%d",&u,&v,&w[i]),s=(s+w[i])%p,ad(u,v,i),ad(v,u,i);
Ans[ans=1][0]=s=s*1ll*qp(n-1,p-2)%p;
Dfs(d[1]=1);dfs(1);
for(int i=1;i<=m;++i) if(w[i]){printf("-1\n");return 0;}
printf("%d\n",ans);
for(int i=1,j;i<=ans;++i,putchar('\n')) for(printf("%d ",Ans[i][0]),j=1;j<n;++j) printf("%d ",Ans[i][j]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 1736kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
5 60 3 2 30 1 2 71 3 2 81 1 2 20 1 3
result:
ok Participant found an answer (5 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 1ms
memory: 1672kb
input:
2 2 37 1 2 8 1 2 15
output:
3 23 2 8 1 29 2
result:
ok Participant found an answer (3 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 1ms
memory: 1652kb
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: -100
Wrong Answer
time: 1ms
memory: 1704kb
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:
29 235 12 2 3 8 14 15 13 10 4 316 9 2 3 8 14 15 13 10 4 681 12 2 3 8 14 15 13 10 4 265 9 2 3 8 14 15 13 10 4 732 9 12 2 3 8 14 15 10 4 125 9 2 3 8 14 15 13 10 4 872 9 12 2 3 8 14 15 13 10 166 9 2 3 8 14 15 13 10 4 831 9 12 2 3 8 14 15 13 10 459 9 2 3 8 14 15 13 10 4 538 9 12 2 3 8 14 15 13...
result:
wrong answer Operation 5 (1-based) is not a tree