QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#865309#4809. Maximum RangeAuroreWA 29ms25872kbC++233.1kb2025-01-21 16:37:452025-01-21 16:37:45

Judging History

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

  • [2025-01-21 16:37:45]
  • 评测
  • 测评结果:WA
  • 用时:29ms
  • 内存:25872kb
  • [2025-01-21 16:37:45]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int buf[30];
inline void print(int x,char ch=' '){
	if(x<0) putchar('-'),x=-x;
	int tot=0;
	do{
		buf[++tot]=x%10;
		x/=10;
	}while(x);
	for(int i=tot;i;i--)
		putchar(buf[i]+'0');
	putchar(ch);
}
const int MAXN=1e5+5,inf=1e18;
int n,m;
int head[MAXN],to[MAXN<<1],nxt[MAXN<<1],val[MAXN<<1],tot=1;
void add(int u,int v,int w){
	nxt[++tot]=head[u];
	to[tot]=v,val[tot]=w;
	head[u]=tot;
}
int dfn[MAXN],low[MAXN],num;
bool del[MAXN<<1];
void tarjan(int x,int y){
	dfn[x]=low[x]=++num;
	for(int i=head[x];i;i=nxt[i]){
		if(!dfn[to[i]]){
			tarjan(to[i],i);
			low[x]=min(low[x],low[to[i]]);
			if(low[to[i]]>dfn[x])
				del[i]=del[i^1]=1;
		}
		else if(i!=(y^1)) 
			low[x]=min(low[x],dfn[to[i]]);
	}
}

int fa[MAXN];
int find(int x){
	return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int u,int v){
	fa[find(u)]=find(v);
} 
int mx[MAXN],mn[MAXN],mxid[MAXN],mnid[MAXN];

struct max_flow{
	int head[MAXN],to[MAXN<<2],nxt[MAXN<<2],flow[MAXN<<2],tot=1;
	void add(int u,int v){
		nxt[++tot]=head[u];
		to[tot]=v,flow[tot]=1;
		head[u]=tot;
		
		nxt[++tot]=head[v];
		to[tot]=u,flow[tot]=0;
		head[v]=tot;
	}
	int pre[MAXN];
	bool vis[MAXN];
	void bfs(){
		queue<int> q;
		q.push(n+1);
		for(int i=1;i<=n+2;i++)
			vis[i]=0,pre[i]=0;
		vis[n+1]=1;
		while(!q.empty()){
			int x=q.front();
			q.pop();
			for(int i=head[x];i;i=nxt[i]){
				if(!vis[to[i]]&&flow[i]){
					vis[to[i]]=1;
					pre[to[i]]=i;
					q.push(to[i]);
				}
			}
		}
	}
	vector<int> EK(){
		bfs();
		vector<int> ans;
		int pos=n+2;
		while(pos){
			ans.push_back(pos);
			int x=pre[pos];
			flow[x]--,flow[x^1]++;
			pos=to[x^1]; 
		}
		return ans;
	} 
}mf;
void solve(int ansu,int ansv){
	for(int x=1;x<=n;x++)
		for(int i=head[x];i;i=nxt[i])
			if(i!=ansu&&i!=ansv&&i!=(ansu^1)&&i!=(ansv^1))
				mf.add(x,to[i]);
	mf.add(n+1,to[ansu]);
	mf.add(n+1,to[ansu^1]);
	mf.add(to[ansv],n+2);
	mf.add(to[ansv^1],n+2);
	vector<int> ans=mf.EK(),temp;
	for(int i=ans.size()-2;i>0;i--) temp.push_back(ans[i]);
	ans=mf.EK();
	for(int i=1;i<ans.size()-1;i++) temp.push_back(ans[i]);
	print(temp.size(),'\n');
	for(auto x:temp) print(x); 
}
signed main(){
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		int u=read(),v=read(),w=read();
		add(u,v,w),add(v,u,w);
	}
	for(int i=1;i<=n;i++)
		if(!dfn[i]) tarjan(i,0);
	
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int x=1;x<=n;x++)
		for(int i=head[x];i;i=nxt[i])
			if(!del[i]) merge(x,to[i]);
	for(int i=1;i<=n;i++)
		mx[i]=-inf,mn[i]=inf;
	for(int x=1;x<=n;x++)
		for(int i=head[x];i;i=nxt[i])
			if(!del[i]){
				int y=find(x);
				if(val[i]>mx[y])
					mx[y]=val[i],mxid[y]=i;
				if(val[i]<mn[y])
					mn[y]=val[i],mnid[y]=i;
			}
			
	int ans=0,ansu,ansv;
	for(int i=1;i<=n;i++)
		if(mx[i]-mn[i]>ans){
			ans=mx[i]-mn[i];
			ansu=mxid[i];
			ansv=mnid[i];
		}
	print(ans,'\n');
	solve(ansu,ansv);
	return 0;
}

詳細信息

Test #1:

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

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

5
4
3 1 5 4 

result:

ok ok

Test #2:

score: 0
Accepted
time: 29ms
memory: 25828kb

input:

99997 100000
12238 99016 352755196
99016 25485 -412473602
25485 2440 991507552
2440 31171 -181894654
36970 2440 -800167579
2440 41865 -148191946
96629 31171 847888506
36970 95740 395546542
27992 2440 647886610
99016 29557 369124914
80795 27992 -673871966
36970 3509 573208857
57672 29557 874406776
41...

output:

1959330954
37
96048 4697 44442 68883 69259 57672 29557 99016 25485 2440 31171 95092 34883 46301 96778 37694 88289 30288 68523 54073 84997 89628 67966 84407 3463 72825 51491 87712 96230 22074 72089 76022 86665 92617 74677 86274 94991 

result:

ok ok

Test #3:

score: 0
Accepted
time: 29ms
memory: 25832kb

input:

99997 100000
41884 21178 -431811360
41884 42699 -450057006
36523 21178 582079730
21178 96679 615552614
63637 21178 498974417
96679 5108 235820276
75058 41884 220112636
35148 42699 589595309
36523 18002 -637739861
65854 5108 -312755792
45137 41884 -511118771
5108 31311 554050951
25335 35148 -28341059...

output:

1968439328
40
23692 87673 42699 41884 45137 85192 38202 83711 83919 55330 71151 98733 99716 70298 33264 26071 90144 25926 52252 51434 69337 7577 5108 50088 6204 28694 41126 87303 83047 26981 54901 59612 14678 35287 78274 18331 89860 71024 99686 98098 

result:

ok ok

Test #4:

score: 0
Accepted
time: 27ms
memory: 25872kb

input:

99984 99999
33974 29867 335681778
33974 87468 348956829
83048 87468 320849805
29867 69456 -424530698
72457 69456 -950650074
53838 83048 755969166
85914 69456 569454441
51728 87468 -202158773
15970 29867 -865071002
15970 94894 697607001
94894 74694 616318126
33974 11496 -89287579
53838 34365 -6577379...

output:

1985932414
36
50114 35807 13570 79841 32897 75496 85914 55808 57640 58540 79605 55857 61993 46598 303 11395 92249 27866 23026 83298 1652 76013 29864 53838 83048 87468 51728 75902 5449 70238 97838 83656 91542 28078 78849 80694 

result:

ok ok

Test #5:

score: -100
Wrong Answer
time: 28ms
memory: 25624kb

input:

99988 99992
8584 11873 -811540160
68064 11873 -930246087
11873 60056 916668870
68064 82193 -859062523
60056 75072 790866030
27767 75072 357619485
75072 78221 411650300
39636 82193 264106928
6675 60056 933851261
71747 78221 -508471038
11873 92771 -665232168
34402 27767 -906494982
11873 42714 63734230...

output:

1932268861
34
6675 32701 89787 8975 26922 21107 93559 75593 83884 20678 16385 62720 25891 75176 34179 64174 38274 8778 99809 28745 6675 32701 89787 23798 83283 2518 50628 88236 5185 29873 3186 91957 75072 60056 

result:

wrong answer Cycle contains repeated edge 6675-32701