第三章—Java NIO程式設計:(8)零拷貝
阿新 • • 發佈:2022-03-16
分析
不難想到,我們可以從攻擊向裝備連線一條邊,從而跑一個最大匹配就可以得到答案.....了麼?
不要忘了資料範圍,資料範圍是1e6因此,最大資料情況下,我們不停的清空st陣列,時間必炸
那怎麼辦呢?
重點優化
我們加入時間戳的概念,在全域性統計一個now變數,它表示的是當前搜尋到的是第幾個。
而st陣列存則是,上次被掃描到的時候的時間,若不等於當前時間,則說明當前還未被遍歷過。因此完成了對時間的優化
AC_code
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int h[N],ne[N<<1],e[N<<1],idx; int match[N],st[N]; int n,now; void add(int a,int b) { e[idx] = b,ne[idx] = h[a],h[a] = idx++; } bool find(int x) { for(int i=h[x];~i;i=ne[i]) { int j = e[i]; if(st[j]!=now) { st[j] = now; if(!match[j]||find(match[j])) { match[j] = x; return 1; } } } return 0; } int main() { scanf("%d",&n); memset(h,-1,sizeof h); for(int i=1;i<=n;i++) { int a,b;cin>>a>>b; add(a,i),add(b,i); } int ans = 0; for(int i=1;i<=10000;i++) { now++; if(find(i)) ans++; else break; } cout<<ans<<endl; }