Gym - 101667J Strongly Matchable

https://vjudge.net/problem/Gym-101667J
难题,思路:https://acrazyczy.github.io/2018/01/16/Daejeon-2017-J/

#include<bits/stdc++.h>
using namespace std;
const int maxn=220;
typedef long long ll;
const int INF=0x3f3f3f3f;

struct Edge{
	int from,to,cap,flow;
};

struct Dinic{
	int n,m,s,t;
	vector<Edge> edges;
	vector<int> G[maxn];
	bool vis[maxn];
	int d[maxn];
	int cur[maxn];

	void init()
	{
		edges.clear();
        for(int i=1;i<=2*n;i++)G[i].clear();
	}

	void AddEdge(int f,int t,int c)
	{
		edges.push_back((Edge){f,t,c,0});
		edges.push_back((Edge){t,f,0,0});
		m=edges.size();
		G[f].push_back(m-2);
		G[t].push_back(m-1);
	}

	bool bfs()
	{	
		memset(vis,0,sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s]=0;
		vis[s]=1;
		while(!Q.empty())
		{
			int x=Q.front();Q.pop();
			for(int i=0;i<G[x].size();i++)
			{
				Edge& e=edges[G[x][i]];
				if(!vis[e.to] && e.cap>e.flow)
				{
					vis[e.to]=1;
					d[e.to]=d[x]+1;
					Q.push(e.to);
				}
			}
		}
		return vis[t];
	}

	int dfs(int x,int a)
	{
		if(x==t || a==0)return a;
		int flow=0,f;
		for(int& i=cur[x];i<G[x].size();i++)
		{
			Edge& e=edges[G[x][i]];
			if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)))>0)
			{
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if(!a)break;
			}
		}
		return flow;
	}

	int MaxFlow()
	{
		int flow=0;
		while(bfs())
		{
			memset(cur,0,sizeof(cur));
			flow+=dfs(s,INF);
		}
		return flow;
	}
}ans;

int main()
{
    //freopen("input.in","r",stdin);
    int n,m;
    int x[10000],y[10000];
    cin>>n>>m;
    for(int i=1;i<=m;i++)cin>>x[i]>>y[i];
    ans.n=n;
    for(int s=1;s<n;s++)
        for(int t=s+1;t<=n;t++)
        {
            ans.s=s,ans.t=t;
            ans.init();
            for(int i=1;i<=n;i++)ans.AddEdge(i,i+n,i==s||i==t?INF:1);
            for(int i=1;i<=m;i++)ans.AddEdge(x[i]+n,y[i],INF),ans.AddEdge(y[i]+n,x[i],INF);
            if(ans.MaxFlow()<n/2){puts("-1");return 0;}
        }
    puts("1");
    return 0;
}
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页