結果

問題 No.2286 Join Hands
ユーザー lgswdnlgswdn
提出日時 2024-03-17 10:25:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 2,384 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 207,032 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-17 10:25:38
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 6 ms
6,676 KB
testcase_10 AC 16 ms
6,676 KB
testcase_11 AC 13 ms
6,676 KB
testcase_12 AC 16 ms
6,676 KB
testcase_13 AC 14 ms
6,676 KB
testcase_14 AC 12 ms
6,676 KB
testcase_15 AC 13 ms
6,676 KB
testcase_16 AC 11 ms
6,676 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 9 ms
6,676 KB
testcase_19 AC 4 ms
6,676 KB
testcase_20 AC 7 ms
6,676 KB
testcase_21 AC 9 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 4 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 4 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 4 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 2 ms
6,676 KB
testcase_45 AC 2 ms
6,676 KB
testcase_46 AC 2 ms
6,676 KB
testcase_47 AC 4 ms
6,676 KB
testcase_48 AC 4 ms
6,676 KB
testcase_49 AC 4 ms
6,676 KB
testcase_50 AC 4 ms
6,676 KB
testcase_51 AC 2 ms
6,676 KB
testcase_52 AC 4 ms
6,676 KB
testcase_53 AC 4 ms
6,676 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 4 ms
6,676 KB
testcase_56 AC 3 ms
6,676 KB
testcase_57 AC 4 ms
6,676 KB
testcase_58 AC 4 ms
6,676 KB
testcase_59 AC 4 ms
6,676 KB
testcase_60 AC 4 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(a) (a).begin(),(a).end()
#ifdef DEBUG
template<class T>
ostream& operator << (ostream &out,vector<T> a){
	out<<'[';
	for(T x:a)out<<x<<',';
	return out<<']';
}
template<class T>
vector<T> ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
	cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
	cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=5e3+10,INF=1e9;
int sid,T,n,m,vis[N],match[N];
namespace Flow{
	const int V=N*2,E=V*2+N*4;
	int s,t,kk,head[V],d[V],cur[V];
	struct edges{
		int to,c,nex;
	}edge[E];
	void init(int x,int y){
		s=x,t=y,kk=1;
		fill(head+s,head+1+t,0);
	}
	bool chk(int i){
		return !edge[i].c;
	}
	int add(int u,int v,int c){
		// debug(u,v,c);
		edge[++kk]={v,c,head[u]},head[u]=kk;
		edge[++kk]={u,0,head[v]},head[v]=kk;
		return kk-1;
	}
	bool bfs(){
		queue<int>q;
		q.push(s);
		copy(head+s,head+1+t,cur+s);
		fill(d+s,d+1+t,-1),d[s]=0;
		for(int u;!q.empty();){
			u=q.front(),q.pop();
			for(int i=head[u];i;i=edge[i].nex){
				int v=edge[i].to;
				if(edge[i].c&&!~d[v])d[v]=d[u]+1,q.push(v);
			}
		}
		return ~d[t];
	}
	int dfs(int u,int lim=INF){
		if(u==t)return lim;
		int flow=0;
		for(int i=cur[u];i&&flow<lim;i=edge[i].nex){
			cur[u]=i;
			int v=edge[i].to,c=edge[i].c;
			if(!c||d[v]!=d[u]+1)continue;
			int f=dfs(v,min(lim-flow,c));
			if(!f)d[v]=-1;
			edge[i].c-=f,edge[i^1].c+=f,flow+=f;
		}
		return flow;
	}
	int dinic(){
		int flow=0;
		for(;bfs();)flow+=dfs(s);
		return flow;
	}
	void find(){
		for(int u=1;u<=n;u++){
			match[u]=0;
			for(int i=head[u];i;i=edge[i].nex){
				int v=edge[i].to;
				if(v>n&&!edge[i].c)match[u]=v-n;
			}
		}
	}
}
int tag[N];
void get(){
	scanf("%d%d",&n,&m);
	int s=0,t=n+n+1;
	Flow::init(s,t);
	for(int i=1;i<=n;i++){
		Flow::add(s,i,1),Flow::add(i+n,t,1);
	}
	fill(vis,vis+1+n,0);
	for(int u,v;m--;){
		scanf("%d%d",&u,&v);
		Flow::add(u,v+n,1),Flow::add(v,u+n,1);
		vis[u]=vis[v]=1;
	}
	int cnt=Flow::dinic();
	if(cnt!=n-1)return printf("%d\n",cnt*2-n),void();
	Flow::find();
	int u=0,v=0;
	fill(tag,tag+1+n,0);
	for(int i=1;i<=n;i++){
		if(!match[i])u=i;
		else tag[match[i]]=1;
	}
	for(int i=1;i<=n;i++){
		if(!tag[i])v=i;
	}
	if(u!=v||vis[u])printf("%d\n",cnt*2-n);
	else printf("%d\n",cnt*2-2-n);
}
int main(){
	get();
	return 0;
}
0