输出不对求大佬帮忙!!!!!
查看原帖
输出不对求大佬帮忙!!!!!
531207
July14th楼主2024/1/26 14:31
#include<bits/stdc++.h> 
#define ll long long
using namespace std;
ll from[10005],nn[10005],to[10005],cost[10005],idx=1;
ll vis[10005],n,m,k,s,t;
#define INF 0x3f3f3f3f
template<class T>inline void read(T &num){
	char ch;
	while(!isdigit(ch=getchar()));
	num=ch-'0';
	while(isdigit(ch=getchar())){
		num=num*10+ch-'0';
	}
} 
inline void add(const int a,const int b,const int c){
	++idx;
	nn[idx]=from[a];
	from[a]=idx;
	to[idx]=b;
	cost[idx]=c;
}
queue<int>q;
bool bfs(){
	memset(vis,0,sizeof(vis));
	while(q.size()){
		q.pop();
	}
	q.push(s);
	vis[s]=1;
	while(q.size()){
		ll x=q.front();
		q.pop();
		for(ll i=from[x];i;i=nn[i]){
			ll y=to[i]; 
			if(!vis[y]&&cost[i]){
				vis[y]=vis[x]+1;
				if(y==t){
					return true;
				}
				q.push(y); 
			}
		}
	}
	return false;
}
int dfs(const int x,const int flow){
	if(x==t){
		return flow;
	}
	ll rest=flow;
	ll k;
	for(int i=from[x];i&&rest;i=nn[i]){
		int y=to[i];
		if(cost[i]&&vis[y]==vis[x]+1){
			k=dfs(y,min(rest,cost[i]));
			if(k){
				cost[i]-=k;
				cost[i^1]+=k;
				rest-=k;
			}
			else{
				vis[y]=0;
			}
		}
	}
	return flow-rest;
}
int dinic(){
	int maxflow=0;
	int flow;
	while(bfs()){
		while(flow=dfs(s,INF)){
			maxflow+=flow;
		}
	}
	return maxflow;
}
inline void print(const int x){
	for(int i=from[x];i;i=nn[i]){
		int y=to[i];
		if(cost[i]&&y<=n){
			cout<<" "<<y;
		}
	}
}
int main(){
	read(k);
	read(n);
	s=n+k+1;
	t=s+1;
	ll x;
	for(int i=1;i<=k;++i){
		read(x);
		m+=x;
		add(n+i,t,x);
		add(t,n+i,0);
	}
	
	for(int i=1;i<=n;++i){
		add(s,i,1); 
		add(i,s,0);
		read(x);
		ll y;
		for(int j=1;j<=x;++j){
			read(y);
			add(i,y+n,1);
			add(y+n,i,0);
		}
	}
	if(dinic()==m){
		for(int i=1;i<=k;++i){
			cout<<i<<":";
			print(n+i);
			cout<<endl;
		}
	}
	else{
		cout<<"No Solution!"<<endl;
	}
	return 0;
}
2024/1/26 14:31
加载中...