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=5e5+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];
  28. int dist[N];
  29.  
  30. bool check()
  31. {
  32. queue<int> q;
  33. for(int u=1;u<=n;++u)
  34. {
  35. if(!matchy[u]) dist[u]=0,q.push(u);
  36. else dist[u]=-1;
  37. }
  38. bool found=0;
  39. while(!q.empty())
  40. {
  41. int u=q.front();q.pop();
  42. for(int v:adj[u])
  43. {
  44. if(!matchx[v]) found=1;
  45. else if(dist[matchx[v]]==-1)
  46. {
  47. dist[matchx[v]]=dist[u]+1;
  48. q.push(matchx[v]);
  49. }
  50. }
  51. }
  52. ret found;
  53. }
  54.  
  55. bool dfs(int u)
  56. {
  57. for(int v:adj[u])
  58. {
  59. if(!matchx[v] || (dist[matchx[v]]==dist[u]+1 && dfs(matchx[v])))
  60. {
  61. matchx[v]=u;
  62. matchy[u]=v;
  63. ret 1;
  64. }
  65. }
  66. dist[u]=-1;
  67. ret 0;
  68. }
  69.  
  70. void sol()
  71. {
  72. cin>>n>>m;
  73. int i0,j0;
  74. vector<pair<int, int>> E;
  75. while(cin>>i0>>j0) adj[i0].pb(j0);
  76. int res=0;
  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 u=1;u<=m;++u) cout<<matchx[u]<<' ';
  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 0.01s 13888KB
stdin
Standard input is empty
stdout
0