可以跑二分图

到第一个不能匹配的点就退出

嗯 还有并查集判环的做法?

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<string>
 7 
 8 using namespace std;
 9 
10 void setIO(const string& a) {
11     freopen((a+".in").c_str(), "r", stdin);
12     freopen((a+".out").c_str(), "w", stdout);
13 }
14 
15 const int N = 1000000 + 10, M = 10000 + 10;
16 
17 #include<vector>
18 vector<int> G[M];
19 int mat[M];
20 
21 void AddEdge(int u, int v) {
22     G[u].push_back(v);
23 }
24 
25 int vis[N], clk;
26 
27 bool find(int u) {
28     for(unsigned i = 0; i < G[u].size(); i++) {
29         int v = G[u][i];
30         if(vis[v] != clk) {
31             vis[v] = clk;
32             if(mat[v] == 0 || find(mat[v])) {
33                 mat[v] = u;
34                 return 1;
35             }
36         }
37     }
38     return 0;
39 }
40 
41 int work(int n) {
42     for(int i = 1; i <= n; i++) {
43         ++clk;
44         if(!find(i)) return i - 1;
45     }
46     return n;
47 }
48 
49 int main() {
50     int x, y, n;
51     scanf("%d", &n);
52     for(int i = 1; i <= n; i++) {
53         scanf("%d%d", &x, &y);
54         AddEdge(x, i);
55         AddEdge(y, i);
56     }
57     printf("%d\n", work(n));
58         
59     return 0;
60 }

 

相关文章:

  • 2022-12-23
  • 2022-01-29
  • 2021-06-13
  • 2021-07-24
  • 2022-12-23
  • 2022-02-20
  • 2021-12-30
猜你喜欢
  • 2021-06-13
  • 2021-10-22
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案