結果
| 問題 |
No.2286 Join Hands
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-03-17 10:25:32 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 44 ms / 2,000 ms |
| コード長 | 2,384 bytes |
| コンパイル時間 | 1,918 ms |
| コンパイル使用メモリ | 198,560 KB |
| 最終ジャッジ日時 | 2025-02-20 07:00:30 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 58 |
コンパイルメッセージ
main.cpp: In function ‘void get()’:
main.cpp:92:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
92 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:100:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#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;
}