LA 7456 Least Crucial Node

题意:有一个联通图G(V,E), |V| <= 100,其中一个vertex是源点,即sink,由sink沿图向其他点发送信息。当你disable了一个点(非sink),阻断了通过该点的信息传输,会导致一些点无法收到信息;一个点被禁用后,导致无法收到信息点数越多,就越crucial。让你求出最crucial而且数字编号最小的点。


方法:DFS

图非常的小,边数最多1e4条,我们只需循环每一个非sink的点,关闭它,然后从sink 开始dfs,求联通分量的大小。

这样做的time complexity是O(n*(|V|+|E|)), 一个case的计算量在1e6左右,不会超过10个testcase,可行。


Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>

#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline '\n'

#define test if(1)cout
using namespace std;

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);



const int maxn = 101;
int n, m, sink;
vector<int> g[maxn];
bitset<maxn> vis;
void dfs(int cur)
{
    vis[cur] = true;
    for (auto nxt : g[cur])
    {
        if (!vis[nxt])
        {
            dfs(nxt);
        }
    }
}

int main()
{
    while (cin >> n && n)
    {
        repn(i, n)  g[i].clear();
        cin >> sink >> m;
        rep(i, m)
        {
            int u, v;   cin >> u >> v;
            g[u].pb(v);
            g[v].pb(u);
        }
        int ans = -1;
        int cnt = n+1;
        repn(i, n)
        {
            if (i == sink)  continue;
            vis.reset();
            vis[i] = true;
            dfs(sink);

            int temp = vis.count();
            if (temp == n)  continue;
        
            if (cnt > temp)
            {
                ans = i;
                cnt = temp;
            }
        }
        cout << ans << newline;
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值