fork download
  1. //Collecting the dream I threw away
  2. //Road to VOI
  3. #include<bits/stdc++.h>
  4. #define el '\n'
  5. #define ll long long
  6. #define ull unsigned long long
  7. #define fumo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  8. #define pb push_back
  9. #define str string
  10. #define sz(v) (int)v.size()
  11. #define ldb long double
  12. #define db double
  13. #define reimu(v) v.begin(), v.end()
  14. #define ret return
  15. #define fi first
  16. #define se second
  17. //#define int long long
  18. using namespace std;
  19. #pragma GCC optimize ("Ofast")
  20. #pragma GCC optimize ("unroll-loops")
  21. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  22. //ᗜˬᗜ
  23. const int N=1e5+7,base=37,block=500;
  24. const ll mod=1e9+9,inf=1e18;
  25. int n,m;
  26. vector<int> adj[N];
  27. int matchx[N],matchy[N],D[N];
  28. int res;
  29.  
  30. bool check()
  31. {
  32. queue<int> q;
  33. for(int u=1;u<=n;++u)
  34. {
  35. if(!matchy[u])
  36. {
  37. D[u]=0;
  38. q.push(u);
  39. }
  40. else D[u]=-1;
  41. }
  42. bool found=0;
  43. while(!q.empty())
  44. {
  45. int u=q.front();
  46. q.pop();
  47. for(int v:adj[u])
  48. {
  49. if(!matchx[v]) found=1;
  50. else if(D[matchx[v]]==-1)
  51. {
  52. D[matchx[v]]=D[u]+1;
  53. q.push(matchx[v]);
  54. }
  55. }
  56. }
  57. ret found;
  58. }
  59.  
  60. bool dfs(int u)
  61. {
  62. for(int v:adj[u]) if(!matchx[v] || (D[matchx[v]]==D[u]+1 && dfs(matchx[v])))
  63. {
  64. matchx[v]=u;
  65. matchy[u]=v;
  66. ret 1;
  67. }
  68. D[u]=-1;
  69. ret 0;
  70. }
  71.  
  72. void sol()
  73. {
  74. cin>>n>>m;
  75. int i0,j0;
  76. while(cin>>i0>>j0) adj[i0].pb(j0);
  77. while(check())
  78. {
  79. for(int u=1;u<=n;++u) if(!matchy[u]) res+=dfs(u);
  80. }
  81. cout<<res<<el;
  82. for(int i=1;i<=m;++i) cout<<matchx[i]<<' ';
  83. }
  84.  
  85. void ffopen(const str &alice)
  86. {
  87. if(fopen((alice+".inp").c_str(), "r")){
  88. freopen((alice+".inp").c_str(), "r", stdin);
  89. //freopen((alice+".out").c_str(), "w", stdout);
  90. }
  91. }
  92.  
  93. signed main()
  94. {
  95. fumo
  96. ffopen("TASK");
  97. int t=1;
  98. //cin>>t;
  99. while(t--) sol();
  100. ret 0;
  101. }
  102.  
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
0