QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#863515#4809. Maximum RangeyhdddWA 25ms23584kbC++203.4kb2025-01-19 18:30:062025-01-19 18:30:15

Judging History

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

  • [2025-01-19 18:30:15]
  • 评测
  • 测评结果:WA
  • 用时:25ms
  • 内存:23584kb
  • [2025-01-19 18:30:06]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=100010;
const int inf=1e18;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
	return x*f;
}
bool Mbe;

int n,m;
pair<pii,int> g[maxn];	
int head[maxn],tot;
struct nd{
	int nxt,to,w;
}e[maxn<<3];
void add(int u,int v,int w){e[++tot]={head[u],v,w};head[u]=tot;}
int dfn[maxn],lw[maxn],idx;
int st[maxn],tp;
int scc[maxn],scct;
void tar(int u){
	dfn[u]=lw[u]=++idx;st[++tp]=u;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(!dfn[v]){
			tar(v);
			lw[u]=min(lw[u],lw[v]);
		}
		else if(!scc[v])lw[u]=min(lw[u],dfn[v]);
	}
	if(dfn[u]==lw[u]){
		scc[st[tp]]=++scct;
		while(st[tp--]!=u)scc[st[tp]]=scct;
	}
}
int mn[maxn],mx[maxn];
void addflow(int u,int v,int w){
	add(u,v,w),add(v,u,0);
}
int s,t,flow;
int dis[maxn],rad[maxn];
queue<int> q;
bool bfs(){
	for(int i=1;i<=t;i++)dis[i]=0,rad[i]=head[i];
	dis[s]=1;q.push(s);
	while(!q.empty()){
		int u=q.front();q.pop();
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].to;
			if(!dis[v]&&e[i].w)dis[v]=dis[u]+1,q.push(v);
		}
	}
	return dis[t];
}
int dfs(int u,int val){
	if(u==t)return val;
	int res=0;
	for(int i=rad[u];i;i=e[i].nxt){
		int v=e[i].to;rad[u]=i;
		if(dis[v]==dis[u]+1&&e[i].w){
			int out=dfs(v,min(e[i].w,val));
			e[i].w-=out,e[i^1].w+=out;
			val-=out,res+=out;
			if(!val)break;
		}
	}
	return res;
}
vector<int> res,id;
bool vis[maxn],bk[maxn];
void get(int u){
	if(u<=n){
		if(vis[u]){
			while(res.back()!=u)res.pop_back(),id.pop_back();
		}
		else res.pb(u),vis[u]=1;
	}
	if(u==t)return ;
	for(int i=rad[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(bk[i])continue;
		if(!e[i].w){
			rad[u]=e[i].nxt;id.pb(i);
			return get(v);
		}
	}
}
void work(){
	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);
		g[i]={{u,v},w};
	}
	tar(1);
	for(int i=1;i<=scct;i++)mn[i]=inf,mx[i]=-inf;
	for(int i=1;i<=n;i++)head[i]=0;tot=1;
	for(int i=1;i<=m;i++){
		auto[p,w]=g[i];auto[u,v]=p;
		if(scc[u]!=scc[v])continue;
		mn[scc[u]]=min(mn[scc[u]],w),mx[scc[u]]=max(mx[scc[u]],w);
	}
	pii ans={0,0};for(int i=1;i<=scct;i++)ans=max(ans,{mx[i]-mn[i],i});
	s=n+1,t=n+2;
	for(int i=1;i<=m;i++){
		auto[p,w]=g[i];auto[u,v]=p;
		if(scc[u]!=scc[v]||scc[u]!=ans.se)continue;
		addflow(u,v,1),addflow(v,u,1);
		if(w==mn[scc[u]]){
			addflow(s,u,1),addflow(s,v,1);
			mn[scc[u]]=inf;
		}
		if(w==mx[scc[u]]){
			addflow(u,t,1),addflow(v,t,1);
			mx[scc[u]]=-inf;
		}
	}
	for(int i=1;i<=2;i++)if(bfs())flow+=dfs(s,1);
	// for(int i=2;i<=tot;i+=2)cout<<e[i^1].to<<" "<<e[i].to<<" "<<e[i].w<<"\n";
	// cout<<flow<<"\n";
	for(int i=1;i<=t;i++)rad[i]=head[i];
	get(s);reverse(res.begin(),res.end());
	for(int i:id)bk[i]=1;
	for(int i=1;i<=n;i++)vis[i]=0;
	for(int i=1;i<=t;i++)rad[i]=head[i];
	get(s);
	printf("%lld\n%lld\n",ans.fi,res.size());
	for(int i:res)printf("%lld ",i);
}

// \
444

bool Med;
int T;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	
//	cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
	
	T=1;
	while(T--)work();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 25ms
memory: 23584kb

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:

1999987695
0

result:

wrong answer Integer 0 violates the range [1, 100000]