洛谷 P2319 [HNOI2006]超级英雄(二分图匹配)

传送门


解题思路

其实这道题和上一道题很像[SCOI2010]连续攻击游戏

这道题就是题目和锦囊连边,然后题目从1开始往后枚举,注意也是如果一道答错就结束,输出答案。

然后注意n是锦囊,m是题目,别弄反了。

(ps:弄反了也能得90分!!)

(数据真水!)

AC代码

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=1005;
 5 int cnt,p[maxn],ok[maxn],vis[maxn],n,m,ans[maxn];
 6 struct edge{
 7     int v,next;
 8 }e[maxn*2];
 9 void insert(int u,int v){
10     cnt++;
11     e[cnt].v=v;
12     e[cnt].next=p[u];
13     p[u]=cnt;
14 }
15 bool work(int u){
16     for(int i=p[u];i!=-1;i=e[i].next){
17         int v=e[i].v;
18         if(vis[v]) continue;
19         vis[v]=1;
20         if(!ok[v]||work(ok[v])){
21             ok[v]=u;
22             return true;
23         }
24     }
25     return false;
26 }
27 int main()
28 {
29     memset(p,-1,sizeof(p));
30     cin>>n>>m;
31     for(int i=1;i<=m;i++){
32         int v1,v2;
33         cin>>v1>>v2;
34         if(v1!=v2){
35             insert(i,v1+1);
36             insert(i,v2+1);    
37         }else{
38             insert(i,v1+1);
39         }
40     }
41     for(int i=1;i<=m+1;i++){
42         memset(vis,0,sizeof(vis));
43         if(!work(i)){
44             cout<<i-1<<endl;
45             for(int j=1;j<=n;j++){
46                 ans[ok[j]]=j;
47             }
48             for(int j=1;j<i;j++){
49                 cout<<ans[j]-1<<endl; 
50             }
51             return 0;
52         }
53     }
54     return 0;
55 }

//HNOI2006 Day1t1

posted @ 2020-01-31 19:24  尹昱钦  阅读(140)  评论(0编辑  收藏  举报