LA 7456 Least Crucial Node

来源:互联网 发布:mysql并发insert死锁 编辑:程序博客网 时间:2024/05/16 06:08

题意:有一个联通图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)coutusing 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;    }}


0 0