QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#90232#5208. Jumbled TreeslijunyiWA 2ms3712kbC++232.1kb2023-03-22 15:18:232023-03-22 15:18:24

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-22 15:18:24]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3712kb
  • [2023-03-22 15:18:23]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int maxn=2500;
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=-1,sum=0,Fa[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);
	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])
		{
			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;
		}
	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;
	dfs1(1,0,0);
	if(bs==-1)
		puts("nmsl"),bs=1ll*s*power(n-1,mod-2)%mod;
	for(int i=1;i<=m;i++)
		if(kd[i])
		{
			dfs2(i,0);
			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; 
}

详细

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 3712kb

input:

3 3 101
1 2 30
2 3 40
3 1 50

output:

nmsl
3
20 1 3 
10 1 2 
30 2 3 

result:

wrong output format Expected integer, but "nmsl" found