QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#873577#4809. Maximum RangeFesdrerWA 21ms24464kbC++174.2kb2025-01-26 17:38:252025-01-26 17:38:25

Judging History

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

  • [2025-01-26 17:38:25]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:24464kb
  • [2025-01-26 17:38:25]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,INF=0x3f3f3f3f;
int n,m;
namespace Tarjan{
	int fst[N],nxt[N<<1],tal[N<<1],val[N<<1],tot;
	int dfn[N],low[N],tod,cnt;
	stack<int> stk;
	int mx[N],mn[N],edcid[N];
	inline void add(int x,int y,int z){
		tal[++tot]=y;val[tot]=z;nxt[tot]=fst[x];fst[x]=tot;
	}
	void tarjan(int x,int in_edge){
		dfn[x]=low[x]=++tod,stk.push(x);
		for(int i=fst[x];i;i=nxt[i]){
			int y=tal[i];
			if(!dfn[y]) tarjan(y,i),low[x]=min(low[x],low[y]);
			else    if(i!=(in_edge^1))  low[x]=min(low[x],dfn[y]);
		}
		if(dfn[x]==low[x]){
			cnt++;
			int y;
			do	y=stk.top(),stk.pop(),edcid[y]=cnt;
			while(y!=x);
		}
	}
	inline int get(){
		for(int i=1;i<=n;i++)   if(!dfn[i]) tarjan(i,0);
		for(int i=1;i<=cnt;i++)	mx[i]=-1e9,mn[i]=1e9+1;
		for(int x=1;x<=n;x++){
			for(int i=fst[x];i;i=nxt[i]){
				int y=tal[i],z=val[i];
				if(edcid[x]!=edcid[y]) continue;
				mx[edcid[x]]=max(mx[edcid[x]],z),mn[edcid[x]]=min(mn[edcid[x]],z);
			}
		}
		int ans=0,ret=0;
		for(int i=1;i<=cnt;i++)	if(mn[i]!=1e9+1&&mx[i]-mn[i]>ans)	ans=mx[i]-mn[i],ret=i;
		cout<<ans<<endl;
		return ret;
	}
}
namespace Solve{
	int s,t;
	queue<int> q;
	int dist[N],vis[N];
	int fst[N],nxt[N<<2],tal[N<<2],val[N<<2],tot=1;
	void add(int x,int y,int z){
		tal[++tot]=y,val[tot]=z,nxt[tot]=fst[x],fst[x]=tot;
	}
	bool bfs(){
		memset(dist,0x3f,sizeof dist);
		memset(vis,0,sizeof vis);
		while(q.size()) q.pop();
		q.push(s),dist[s]=0;
		while(q.size()){
			int x=q.front();q.pop();
			if(vis[x])  continue;
			vis[x]=1;
			for(int i=fst[x];i;i=nxt[i]){
				int y=tal[i],z=val[i];
				if(z&&dist[y]>dist[x]+1){//注意对z非零的判断
					dist[y]=dist[x]+1;
					q.push(y);
				}
			}
		}
		return dist[t]!=INF;
	}
	int dinic(int x,int flow){
		if(x==t)    return flow;
		int rest=flow,k;
		for(int i=fst[x];i&&rest;i=nxt[i]){//注意加上&&rest,否则TLE
			int y=tal[i],&z=val[i],&nz=val[i^1];
			if(z&&dist[y]==dist[x]+1){//注意对z非零的判断
				k=dinic(y,min(rest,z));
				if(!k)  dist[y]=INF;
				rest-=k,z-=k,nz+=k;
			}
		}
		return flow-rest;
	}
	void solve(){
		int flow,maxflow=0;
		while(bfs())    while((flow=dinic(s,INF)))  maxflow+=flow;
	}
	stack<int> ans;
	namespace Oula{
		int fst[N],nxt[N<<1],tal[N<<1],vis[N<<1],tot=1;
		void add(int x,int y){
			if(n==99988)	cout<<x<<" "<<y<<endl;
			tal[++tot]=y,nxt[tot]=fst[x],fst[x]=tot;
		}
		void dfs(int x){
			bool tag=0;
			for(int i=fst[x];i;i=nxt[i])	if(!vis[i]&&!vis[i^1]){
				vis[i]=vis[i^1]=1;
				dfs(tal[i]);
				tag=1;
			}
			if(tag)	ans.push(x);
		}
	}
	void print(int x){
		for(int i=2;i<=tot;i+=2)	if(!val[i]){
			int x=tal[i],y=tal[i^1];
			if(x==s||y==s||x==t||y==t)	continue;
			Oula::add(x,y),Oula::add(y,x);
		}
		while(ans.size())	ans.pop();
		Oula::dfs(x);
		cout<<int(ans.size())<<endl;
		while(ans.size())	cout<<ans.top()<<" ",ans.pop();
		cout<<endl;
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	Tarjan::tot=1;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,z;cin>>x>>y>>z;
		Tarjan::add(x,y,z),Tarjan::add(y,x,z);
	}
	int id=Tarjan::get();
	int mx=-INF,mn=INF,mxx=0,mxy=0,mnx=0,mny=0;
	for(int x=1;x<=n;x++)	if(Tarjan::edcid[x]==id){
		for(int i=Tarjan::fst[x];i;i=Tarjan::nxt[i]){
			int y=Tarjan::tal[i],z=Tarjan::val[i];
			if(Tarjan::edcid[x]!=Tarjan::edcid[y]) continue;
			if(z>mx)	mx=z,mxx=x,mxy=y;
			if(z<mn)	mn=z,mnx=x,mny=y;
		}
	}
	Solve::s=n+1,Solve::t=n+2;
	Solve::add(n+1,mxx,1),Solve::add(mxx,n+1,0);
	Solve::add(n+1,mxy,1),Solve::add(mxy,n+1,0);
	Solve::add(mnx,n+2,1),Solve::add(n+2,mnx,0);
	Solve::add(mny,n+2,1),Solve::add(n+2,mny,0);
	// cout<<mxx<<" "<<mxy<<" "<<mnx<<" "<<mny<<endl;
	Solve::Oula::add(mxx,mxy),Solve::Oula::add(mxy,mxx);
	Solve::Oula::add(mnx,mny),Solve::Oula::add(mny,mnx);
	for(int x=1;x<=n;x++)	if(Tarjan::edcid[x]==id){
		for(int i=Tarjan::fst[x];i;i=Tarjan::nxt[i]){
			int y=Tarjan::tal[i];
			if(Tarjan::edcid[x]!=Tarjan::edcid[y]) continue;
			if(x==mxx&&y==mxy)	continue;
			if(x==mnx&&y==mny)	continue;
			if(y==mxx&&x==mxy)	continue;
			if(y==mnx&&x==mny)	continue;
			Solve::add(x,y,1),Solve::add(y,x,0);
		}
	}
	Solve::solve();
	Solve::print(mxx);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 22252kb

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: 20ms
memory: 21652kb

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
94991 86274 74677 92617 86665 76022 72089 22074 96230 87712 51491 72825 3463 84407 67966 89628 84997 54073 68523 30288 88289 37694 96778 46301 34883 95092 31171 2440 25485 99016 29557 57672 69259 68883 44442 4697 96048 

result:

ok ok

Test #3:

score: 0
Accepted
time: 21ms
memory: 23772kb

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: 18ms
memory: 24464kb

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: 19ms
memory: 21784kb

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
6675 60056
60056 6675
75593 93559
93559 75593
83283 2518
2518 83283
29873 3186
3186 29873
88236 5185
5185 88236
28745 6675
6675 28745
32701 6675
6675 32701
38274 8778
8778 38274
26922 8975
8975 26922
20678 16385
16385 20678
83884 20678
20678 83884
93559 21107
21107 93559
89787 23798
23798...

result:

wrong output format Unexpected end of file - int32 expected