FZU ~APTX4869

来源:互联网 发布:下载软件的网站 编辑:程序博客网 时间:2024/05/07 07:01
<pre name="code" class="cpp">#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>using namespace std;/*二分答案+并查集判定 http://acm.fzu.edu.cn/contest/problem.php?cid=151&sortid=5*/ const int maxn = 805;int mx[maxn][maxn];int flag[maxn];int father[maxn];void init(int n) {    for(int i=0; i<n; i++)      father[i] = i;}int getfather(int x) {    if(x != father[x])         father[x] = getfather(father[x]);     return father[x];}bool same(int x,int y) {    return getfather(x)==getfather(y);}void unionAll(int x,int y) {    x = getfather(x);    y = getfather(y);    if(x!= y)       father[x]=y;}bool gao(int n, int x){init(n);for(int i = 0 ; i < n ; i ++){for(int j = i + 1; j < n; j++){if(mx[i][j] < x){unionAll(i,j);}}}int cnt = 0;for(int i =0 ; i < n;i ++){if(father[i] == i){cnt ++;}}return cnt == 1 ? false:true;} int main(){int n , i ,j;while(scanf("%d",&n)!=EOF){int l = 0, r = 0 , mid;for(i = 0 ; i < n; i ++){for(j = 0 ; j < n; j ++){scanf("%d",&mx[i][j]);if(r < mx[i][j]) r = mx[i][j];}}int ans = 0;while(l <= r){mid = (l + r) >>1;if(gao(n , mid)){ans = mid;l = mid + 1;}else{r = mid -1;}}printf("%d\n",ans);}return 0;} 


                                             
0 0