bzoj 1854: [Scoi2010]遊戲(二分圖的最大匹配)
阿新 • • 發佈:2019-01-22
1854: [Scoi2010]遊戲
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3695 Solved: 1405
[Submit][Status][Discuss]
Description
lxhgww最近迷上了一款遊戲,在遊戲裡,他擁有很多的裝備,每種裝備都有2個屬性,這些屬性的值用[1,10000]之間的數表示。當他使用某種裝備時,他只能使用該裝備的某一個屬性。並且每種裝備最多隻能使用一次。 遊戲進行到最後,lxhgww遇到了終極boss,這個終極boss很奇怪,攻擊他的裝備所使用的屬性值必須從1開始連續遞增地攻擊,才能對boss產生傷害。也就是說一開始的時候,lxhgww只能使用某個屬性值為1的裝備攻擊boss,然後只能使用某個屬性值為2的裝備攻擊boss,然後只能使用某個屬性值為3的裝備攻擊boss……以此類推。 現在lxhgww想知道他最多能連續攻擊boss多少次?Input
Output
輸出一行,包括1個數字,表示lxhgww最多能連續攻擊的次數。Sample Input
31 2
3 2
4 5
Sample Output
2HINT
【資料範圍】
對於30%的資料,保證N < =1000
對於100%的資料,保證N < =1000000
Source
題解: 二分圖的最大匹配.因為所有屬性的權值只用選擇一次,而所有武器只能選擇其中的一個屬性,所有從一個武器的兩個屬性分別向這個武器連邊,跑匈牙利演算法即可.(貌似網上有寫並查集的).
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define N 3200000 #define M 1200000 #define inf 1000000000 using namespace std; int next[N*2],v[N*2],remain[N*2]; int point[M]; int belong[M],cur[M]; int n,tot; void add(int x,int y) { tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; } int dfs(int x) { for (int i=point[x];i;i=next[i]) { int t=v[i]; if (cur[t]==i) continue; cur[t]=i; if (!belong[t]||dfs(belong[t])) { belong[t]=x; return true; } } return false; } int main() { scanf("%d",&n); int maxn=0; for (int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); add(x,i); add(y,i); maxn=max(maxn,x); maxn=max(maxn,y); } memset(belong,0,sizeof(belong)); for (int i=1;i<=maxn;i++) { if (!dfs(i)) { printf("%d\n",i-1); return 0; } } printf("%d\n",maxn); }