【图论】[ACM]嗅探器

就是按照一个点作为根然后再这上面搞一下寻找割点,如果找到了,判断另一个点是不是在当前的割点的子树中(是相对于当前子树而言是一个割点)。如果在,那么就更新ans为最小值就行了

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 100;
const int MAXM = 10000;
struct node{
    int v;
    node *next;
}Edges[MAXM * 2 + 10], *adj[MAXN+10], *ecnt=Edges;
int ver, ans=1000000000;
void addedge(int u, int v){
    ++ecnt;
    ecnt->v = v;
    ecnt->next = adj[u];
    adj[u] = ecnt;
}
vector<int> vec;
int dfn[MAXN+10], low[MAXN+10],  dcnt, root;
int cut[MAXN+10];
bool have[MAXN+10];
void dfs(int u, int fa){
    dfn[u] = low[u] = ++dcnt;
    have[u] = u == ver;
    for(node *p=adj[u];p;p=p->next){
        int v= p->v;
        if(!dfn[v]){
            dfs(v, u);
            have[u] = have[v] || have[u];
            low[u] = min(low[u], low[v]);
            if(low[v] >= dfn[u] && fa != -1){
                if(have[v])
                    ans = min(ans, u);
            }
        }else if(v != fa)
            low[u] = min(low[u], dfn[v]);
    }
}
int main(){
    int n, u;
    scanf("%d", &n);
    while(scanf("%d%d", &u, &ver)&&u&&ver){
        addedge(u, ver);
        addedge(ver, u);
    }
    scanf("%d%d", &u, &ver);
    dfs(u, -1);
    if(ans != 1000000000){
        printf("%d\n", ans);
        return 0;
    }
    printf("No solution\n");

    return 0;
}

posted on 2015-07-22 09:11  JeremyGuo  阅读(155)  评论(0编辑  收藏  举报

导航