QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#118548#4303. New Levellyx123886a123886WA 3ms13848kbC++142.0kb2023-07-03 17:10:212023-07-03 17:10:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-03 17:10:23]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:13848kb
  • [2023-07-03 17:10:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int read() {
	char ch=getchar();int res=0,fl=1;
	while(ch<'0'||ch>'9'){if(ch=='-') fl=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){res=res*10+ch-'0';ch=getchar();}
	return res*fl;
}
#define ll long long 
struct node{
	int u;
	ll dis;
};
bool operator <(node A,node B) {return A.dis>B.dis;}
priority_queue<node> q;
const int N=500050;
#define inf 1000000000000
ll dis[N];int vis[N];
int nex[N<<1],fir[N],v[N<<1],w[N<<1],__cnt=0,c[N],n,mod,m;
void addedge(int x,int y,int z) {
	nex[++__cnt]=fir[x];
	fir[x]=__cnt;
	v[__cnt]=y;
	w[__cnt]=z;
//	printf("(%d,%d,%d)\n",x,y,z);
	return ;
}
void dijkstra(int S) {
	for(int i=1;i<=n;++i) dis[i]=inf;
	dis[S]=0;
//	vis[S]=1;!!
	q.push((node){S,dis[S]});
	while(!q.empty()) {
		int u=q.top().u;
		q.pop();
		if(vis[u]) continue;
		vis[u]=1;
//		printf("%d",u);
		for(int i=fir[u];i;i=nex[i]) {
			int to=v[i];
//			printf("%d",to);
			if(dis[to]>dis[u]+w[i]) {
				dis[to]=dis[u]+w[i];
				q.push((node){u,dis[u]}); 
			}
		}
	}
	return ;
}

int fa[N];
int get_fa(int x) {return (fa[x]==x)?x:get_fa(fa[x]);}
int merge(int x,int y) {
	x=get_fa(x);y=get_fa(y);
	if(x==y) return 0;
	fa[x]=y;
	return 1;
}
struct edge{
	int u,v;
}e[N];
int tot=0;
int d(int x,int y){return (c[y]-c[x]-1+mod)%mod;}//x->y
int main()
{
	//freopen("past.in","r",stdin);
	//freopen("past.out","w",stdout);
	n=read();m=read();mod=read();
	for(int i=1;i<=n;++i) c[i]=read()-1;
	for(int i=1;i<=n;++i) fa[i]=i;//!
	for(int i=1;i<=m;++i) {
		e[i].u=read();
		e[i].v=read();
		if(!d(e[i].u,e[i].v)||!d(e[i].v,e[i].u)) merge(e[i].u,e[i].v);
	}
	for(int i=1;i<=m;++i) {
		int fx=get_fa(e[i].u),fy=get_fa(e[i].v);
		if(fx==fy) continue;
		addedge(fx,fy,d(e[i].u,e[i].v)); 
		addedge(fy,fx,d(e[i].v,e[i].u));
	}
	dijkstra(get_fa(1));
//	for(int i=1;i<=n;++i) printf("%d,%lld ",get_fa(i),dis[get_fa(i)]);
//	printf("\n");
	for(int i=1;i<=n;++i) printf("%lld ",((c[i]+inf-dis[get_fa(i)])%mod+mod)%mod+1);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13848kb

input:

4 4 4
1 2 3 1
1 2
1 3
2 3
3 4

output:

1 2 3 4 

result:

ok n=4, m=4, k=4

Test #2:

score: 0
Accepted
time: 2ms
memory: 9728kb

input:

10 9 3
3 2 3 3 1 2 3 1 1 2
2 1
3 2
4 2
5 3
6 5
7 6
8 6
9 7
10 9

output:

1 3 1 1 2 3 1 2 2 3 

result:

ok n=10, m=9, k=3

Test #3:

score: -100
Wrong Answer
time: 3ms
memory: 13820kb

input:

239 238 10
6 1 2 10 9 1 8 10 1 10 6 4 5 2 7 8 4 9 7 5 1 3 2 8 1 7 3 4 6 4 2 6 3 10 3 10 5 1 8 8 1 1 2 3 5 5 5 9 3 8 3 4 7 10 7 5 7 8 2 6 8 10 3 3 2 1 7 5 1 4 4 1 9 9 4 2 10 1 6 10 5 3 8 4 4 10 4 4 2 9 9 6 6 8 2 3 2 4 8 5 10 10 3 3 5 1 4 8 4 2 3 6 10 4 10 2 8 2 2 5 7 5 3 3 8 1 7 10 2 8 2 6 3 10 6 5 9...

output:

6 7 8 7 9 7 8 10 8 10 6 4 5 2 7 8 4 9 7 5 1 3 2 8 1 7 3 4 6 4 2 6 3 10 3 10 5 1 8 8 1 1 2 3 5 5 5 9 3 8 3 4 7 10 7 5 7 8 2 6 8 10 3 3 2 1 7 5 1 4 4 1 9 9 4 2 10 1 6 10 5 3 8 4 4 10 4 4 2 9 9 6 6 8 2 3 2 4 8 5 10 10 3 3 5 1 4 8 4 2 3 6 10 4 10 2 8 2 2 5 7 5 3 3 8 1 7 10 2 8 2 6 3 10 6 5 9 5 1 9 4 6 3...

result:

wrong answer Vertices 7 and 3 have the same color