UVALive 7456 Least Crucial Node

来源:互联网 发布:java异常分类及处理 编辑:程序博客网 时间:2024/04/29 13:08

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5478


题意:有n个点,m条边,给出点S(1~n中的某个点)。除了S点之外的n-1个点中的一个如果失效,那么可能会影响到若干个点和S的连通性。现在要找一个点p,使得p失效后使得点和S不连通的数量最大,在此基础上p的标号越小。


思路:枚举失效n-1个点,每次做一遍并查集,检查有多少点会失效。然后取最大值即可。


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007#define mp make_pair#define fi first#define se second#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mid int m=(l+r)>>1const int maxn = 110;int n,m,st;int x[maxn*maxn];int y[maxn*maxn];int k[maxn];int father[maxn];int getfather(int x){    return x==father[x]?x:father[x] = getfather(father[x]);}void init(){    scanf("%d",&st);    scanf("%d",&m);    rep(i,1,m) scanf("%d%d",&x[i],&y[i]);    Clean(k,0);}void unio( int a , int b ){    int fa = getfather(a);    int fb = getfather(b);    if ( fa != fb )    {        father[fa] = fb;    }}void solve(){    rep(i,1,n)    {        if ( i == st ) continue;        rep(j,1,n) father[j] = j;        rep(j,1,m)        if ( x[j] != i && y[j] != i )            unio( x[j] , y[j] );        rep(j,1,n)        if ( j != i && getfather(j) != getfather(st) ) k[i]++;    }    int ans,M = 0;    rep(i,1,n)    if ( k[i] > M )    {        M = k[i];        ans = i;    }    printf("%d\n",ans);}int main(){    while( scanf("%d",&n) == 1 )    {        if ( !n ) break;        init();        solve();    }    return 0;}


0 0