UVA 10765 Doves and bombs 割点

最近好懒,堆了好多题没写题解。。

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1706

题意:

给你一个图,问你每个点去掉后有多少个联通块

题解:

就Tarjan一下就好,很简单

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cstdio>
#include<string>
#include<algorithm>
#define MAX_N 11234
using namespace std;

int n,m;
vector<int> G[MAX_N];

int dfn[MAX_N],low[MAX_N],ind=0;
bool vis[MAX_N];

struct node{
    public:
        int pos,val;
        void print(){
            cout<<pos-1<<" "<<val<<endl;
        }
};

bool cmp(node a,node b){
    if(a.val==b.val)return a.pos<b.pos;
    return a.val>b.val;
}

node d[MAX_N];

void Tarjan(int u,int p){
    d[u].pos=u,d[u].val=1;
    dfn[u]=low[u]=++ind;
    vis[u]=1;
    int child=0;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==p)continue;
        if(!vis[v]){
            child++;
            Tarjan(v,u);
            low[u]=min(low[v],low[u]);
            if(low[v]>=dfn[u]&&p!=0)d[u].val++;
        }
        else low[u]=min(dfn[v],low[u]);
    }
    if(p==0&&child>1)d[u].val=child;
}

void init(){
    memset(vis,0,sizeof(vis));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    ind=0;
    for(int i=0;i<=n;i++)G[i].clear();
}

int main(){
    cin.sync_with_stdio(false);
    while(true){
        cin>>n>>m;
        if(n==0&&m==0)break;
        init();
        while(true){
            int u,v;
            cin>>u>>v;
            if(u==-1)break;
            u++;v++;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        Tarjan(1,0);
        sort(d+1,d+1+n,cmp);
        for(int i=1;i<=m;i++)
            d[i].print();
        cout<<endl;
    }
    return 0;
}
时间: 2024-10-09 10:35:27

UVA 10765 Doves and bombs 割点的相关文章

UVA - 10765 Doves and bombs (双联通分量)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34798 给N个点的无向图并且联通,问删除每次一个点之后还剩多少联通分量. 找割顶 如果删除的是割顶 联通分量就会增加,否则还是1(因为原图是联通图),删除割顶之后 联通块的数目 就要看该割顶在几个双联通分量里出现过. #pragma comment(linker, "/STACK:10240000,10240000") #include <a

uva 10765 Doves and Bombs(割顶)

?? 题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标记除去该点后有多少个连通分量 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm&

10765 - Doves and bombs(双连通分量)

UVA 10765 - Doves and bombs 题目链接 题意:给定一个无向图,每个点的鸽子值为删去这个点后会有几个连通块,问鸽子值前m大的点的鸽子值,如果相同,按编号排 思路:就裸的双连通分量,在每个连通分量如果是割顶的点就加一,最后如果答案为0的点,答案应该是1 代码: #include <cstdio> #include <cstring> #include <vector> #include <stack> #include <algo

Uva 10765 点双连通求删任意点后剩下图中的连通分量数

题目挂在wustoj上,不知道什么原因不能粘贴链接.. 第一题题号为1314..这题是智力题...换成7的阶乘就可以了.. 代码如下. #include<cstdio> int main() { printf("...............................................................................................\n"); printf("..#................

UVa 10765 鸽子和炸弹(割点)

https://vjudge.net/problem/UVA-10765 题意: 给一个n个点的无向图,求每个点删去后形成的连通分量数. 思路: 判断割点,如果是割点的话,在dfs的时候计算出删去它后所形成的连通分量数. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<

【UVA10765】Doves and bombs (BCC求割点后联通块数量)

题目: 题意: 给了一个联通无向图,现在问去掉某个点,会让图变成几个联通块? 输出的按分出的从多到小,若相等,输出标号从小到大.输出M个. 分析: BCC求割点后联通块数量,Tarjan算法. 联通块的数目在找到一个low[y]>=dfn[x]时累加,最后加一即可. 代码如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #inclu

Uva 315 求无向图的割点的个数

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to 

POJ 1144 &amp; Uva 315 Network 【求割点数目】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10855   Accepted: 5020 链接:http://poj.org/problem?id=1144 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places

uva 315 Network(连通图求割点)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251  Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers