QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402024 | #5208. Jumbled Trees | Graphcity | WA | 1ms | 3916kb | C++20 | 2.8kb | 2024-04-29 19:21:41 | 2024-04-29 19:21:41 |
Judging History
answer
#include<bits/stdc++.h>
#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=2e3;
struct Node{int frm,to,nxt,id;} Edge[Maxn*2+5];
int tot,Head[Maxn+5],h[Maxn+5];
inline void Addedge(int x,int y,int id)
{
Edge[++tot]=(Node){x,y,Head[x],id},Head[x]=tot;
Edge[++tot]=(Node){y,x,Head[y],id},Head[y]=tot;
}
int n,m,Mod,X[Maxn+5],Y[Maxn+5],anc[Maxn+5],fat[Maxn+5],sum,cnt,all=-1;
int fa[Maxn+5],dep[Maxn+5],chk[Maxn+5],vis[Maxn+5],deg[Maxn+5];
vector<int> v[Maxn+5];
vector<pair<int,vector<int>>> ans;
inline int Pow(int x,int y)
{
int res=1;
while(y)
{
if(y&1) res=1ll*res*x%Mod;
x=1ll*x*x%Mod,y>>=1;
}
return res;
}
inline void dfs(int x,int f)
{
fat[x]=f,dep[x]=dep[f]+1;
for(int i=Head[x];i;i=Edge[i].nxt)
{
int y=Edge[i].to; if(dep[y]) continue;
fa[y]=Edge[i].id,chk[Edge[i].id]=1,dfs(y,x);
}
}
inline void dfs1(int x)
{
vis[x]=1,sum=(sum+h[x])%Mod,cnt=(cnt+1)%Mod;
for(auto y:v[x]) if(!vis[y]) anc[y]=x,deg[x]++,dfs1(y);
}
inline void Work(int id)
{
sum=0,cnt=Mod-1,dfs1(id);
if(cnt==0 && sum!=0) {cout<<-1<<endl; exit(0);}
if(cnt!=0)
{
int now=1ll*(Mod-sum)*Pow(cnt,Mod-2)%Mod;
if(all!=-1 && all!=now) {cout<<-1<<endl; exit(0);} all=now;
}
}
int main()
{
// freopen("1.in","r",stdin);
cin>>n>>m>>Mod;
For(i,1,m)
{
int x,y; cin>>x>>y>>h[i]; X[i]=x,Y[i]=y;
h[i]=(Mod-h[i])%Mod,Addedge(x,y,i);
}
if(m>=30) cout<<X[m]<<' '<<Y[m]<<' '<<(Mod-h[m])%Mod<<endl;
dfs(1,0);
For(i,1,m) if(!chk[i])
{
int x=X[i],y=Y[i]; if(dep[x]<dep[y]) swap(x,y);
for(int j=x;j!=y;j=fat[j]) v[i].push_back(fa[j]),v[fa[j]].push_back(i);
}
For(i,1,m) if(!vis[i]) Work(i);
if(all)
{
vector<int> w;
For(i,1,m) if(chk[i]) w.push_back(i),h[i]=(h[i]+all)%Mod;
ans.emplace_back(all,w);
}
// memset(vis,0,sizeof(vis));
// For(i,1,m) if(!vis[i])
// {
// sum=cnt=0; dfs1(i);
// assert(!sum);
// }
queue<int> q; For(i,1,m) if(!deg[i]) q.push(i);
while(!q.empty())
{
int x=q.front(),y=anc[x]; q.pop();
if(h[x])
{
vector<int> w1,w2;
int k=(Mod-h[x])%Mod; h[x]=(h[x]+k)%Mod,h[y]=(h[y]-k+Mod)%Mod;
For(i,1,m) if(i!=x && (chk[i] || i==y)) w2.push_back(i);
For(i,1,m) if(i!=y && (chk[i] || i==x)) w1.push_back(i);
ans.emplace_back(k,w1),ans.emplace_back((Mod-k)%Mod,w2);
} if(y && --deg[y]==0) q.push(y);
}
cout<<ans.size()<<endl;
for(auto [k,w]:ans) {cout<<k<<' '; for(auto i:w) cout<<i<<' '; cout<<endl;}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3664kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
5 60 2 3 81 2 3 20 1 3 91 2 3 10 1 2
result:
ok Participant found an answer (5 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
2 2 37 1 2 8 1 2 15
output:
3 23 2 29 2 8 1
result:
ok Participant found an answer (3 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 0ms
memory: 3624kb
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: 0ms
memory: 3656kb
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: 1ms
memory: 3916kb
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:
16 5 9860 61 296 4 6 7 10 12 13 14 15 16 17 18 21 24 25 26 27 28 29 30 6640 4 6 7 10 12 13 14 15 16 17 18 21 24 25 26 27 28 29 30 3333 4 6 10 12 13 14 15 16 17 18 20 21 24 25 26 27 28 29 30 7091 4 6 7 10 12 13 14 15 16 17 18 21 24 25 26 27 28 29 30 2882 1 4 6 7 10 12 14 15 16 17 18 21 24 25 26 2...
result:
wrong answer Integer parameter [name=e[0][0]] equals to 9860, violates the range [1, 30]