思路:二分图匹配;
提交:1次;
题解:如思路;
但是需要多记一个$ans[u]$代表匹配的答案。
#include<cstdio> #include<iostream> #include<cstring> #define ull unsigned long long #define ll long long #define R register int using namespace std; #define pause (for(R i=1;i<=10000000000;++i)) #define In freopen("NOIPAK++.in","r",stdin) #define Out freopen("out.out","w",stdout) namespace Fread { static char B[1<<15],*S=B,*D=B; #ifndef JACK #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) #endif inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);} inline void gs(char* s) { register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar())); } } using Fread::g; using Fread::gs; namespace Luitaryi { const int N=1010; int n,m,cnt,C,tot; int vr[N<<2],nxt[N<<2],fir[N],pre[N],ans[N],vis[N]; inline void add(int u,int v) {vr[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;} inline bool dfs(int u) { for(R i=fir[u];i;i=nxt[i]) { R v=vr[i]; if(vis[v]==C) continue; vis[v]=C; if(!pre[v]||dfs(pre[v])) {pre[v]=u,ans[u]=v; return true;} } return false; } inline void main() { n=g(),m=g(); for(R i=1,x,y;i<=m;++i) x=g(),y=g(),add(i,x),add(i,y); for(R i=1;i<=m;++i) { ++C; if(dfs(i)) ++tot; else break; } printf("%d ",tot); for(R i=1;i<=tot;++i) printf("%d ",ans[i]); } } signed main() { Luitaryi::main(); }
2019.07.21