冗余路径Redundant Paths

题意翻译

题目描述

在Byteotia有n个城镇。 一些城镇之间由无向边连接。 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些)。每两个城镇之间至多只有一条直接连接的道路。人们可以从任意一个城镇直接或间接到达另一个城镇。 每个城镇都有一个公民,他们被孤独所困扰。事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇)。所以,一共会有n*(n-1)次拜访。

不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件。

作为抗议行动,程序员们计划封锁一些城镇,阻止人们进入,离开或者路过那里。

正如我们所说,他们正在讨论选择哪些城镇会导致最严重的后果。

编写一个程序:

读入Byteotia的道路系统,对于每个被决定的城镇,如果它被封锁,有多少访问不会发生,输出结果。

输入输出格式

第一行读入n,m,分别是城镇数目和道路数目

城镇编号1~n

接下来m行每行两个数字a,b,表示a和b之间有有一条无向边

输出n行,每行一个数字,为第i个城镇被锁时不能发生的访问的数量。

翻译提供者:Park

题目描述

There are exactly nn towns in Byteotia.

Some towns are connected by bidirectional roads.

There are no crossroads outside towns, though there may be bridges, tunnels and flyovers. Each pair of towns may be connected by at most one direct road. One can get from any town to any other-directly or indirectly.

Each town has exactly one citizen.

For that reason the citizens suffer from loneliness.

It turns out that each citizen would like to pay a visit to every other citizen (in his host‘s hometown), and do it exactly once. So exactly n\cdot (n-1)n⋅(n−1) visits should take place.

That‘s right, should.

Unfortunately, a general strike of programmers, who demand an emergency purchase of software, is under way.

As an act of protest, the programmers plan to block one town of Byteotia, preventing entering it, leaving it, and even passing through.

As we speak, they are debating which town to choose so that the consequences are most severe.

Task Write a programme that:

reads the Byteotian road system‘s description from the standard input, for each town determines, how many visits could take place if this town were not blocked by programmers, writes out the outcome to the standard output.

给定一张无向图,求每个点被封锁之后有多少个有序点对(x,y)(x!=y,1<=x,y<=n)满足x无法到达y

输入格式

In the first line of the standard input there are two positive integers: nn and mm (1\le n\le 100\ 0001≤n≤100 000, 1\le m\le 500\ 0001≤m≤500 000) denoting the number of towns and roads, respectively.

The towns are numbered from 1 to nn.

The following mm lines contain descriptions of the roads.

Each line contains two integers aa and bb (1\le a<b\le n1≤a<b≤n) and denotes a direct road between towns numbered aa and bb.

输出格式

Your programme should write out exactly nn integers to the standard output, one number per line. The i^{th}ith line should contain the number of visits that could not take place if the programmers blocked the town no. ii.

输入输出样例

输入 #1复制

5 5
1 2
2 3
1 3
3 4
4 5

输出 #1复制

8
8
16
14
8

一道十分适合练习TarjanTarjan的图论题。

对于每一个点,考虑将其删除对图会产生什么影响,进而计算答案。

#include<bits/stdc++.h>
using namespace std;
int ff[10100],nex[10100],to[10100],a,b,cnt,num,bridg[10100],br,u[10100]; 

int dfn[10100],low[10100],f[10100],n,m,size,t,dcc,c[10100],du[10100];

void add(int a,int b)
{
    nex[++t]=ff[a];
    u[t]=a;
    to[t]=b;
    ff[a]=t;
    return ;
}

void tarjan(int u,int edge){
    dfn[u]=low[u]=++num;
    for(int i=ff[u];i!=0;i=nex[i]){
        int v=to[i];
        if(!dfn[v]){
            tarjan(v,i);
            low[u]=min(low[u],low[v]);
            if(dfn[u]<low[v]){
                bridg[i]=bridg[i^1]=1;
            }
        }
        else if(i!=(edge^1)){
            low[u]=min(low[u],dfn[v]);
        }
    }
}
void dfs(int u){
    c[u]=dcc;
    for(int i=ff[u];i!=0;i=nex[i]){
        int v=to[i];
        if(c[v]!=0||bridg[i]==1)
        continue;
        dfs(v);
    }
}

int main(){
    scanf("%d%d",&n,&m);
    t=1;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&a,&b);
        add(a,b);add(b,a);
    }
    tarjan(1,0);
    for(int i=1;i<=n;i++){
        if(!c[i])
        {
            dcc++;
            dfs(i);
        }
    }
    for(int i=1;i<=m;i++){
        if(c[u[i*2]]!=c[to[i*2]])
        {
            du[c[u[i*2]]]++;
            du[c[to[i*2]]]++;
        }
    }
    for(int i=1;i<=dcc;i++){
        if(du[i]==1){
            br++;
        }
    }
    printf("%d",(br+1)/2);
    return 0;
}

原文地址:https://www.cnblogs.com/hrj1/p/11156516.html

时间: 2024-11-05 11:31:57

冗余路径Redundant Paths的相关文章

Luogu2860 [USACO06JAN]冗余路径Redundant Paths

\(\verb|Luogu2860 [USACO06JAN]冗余路径Redundant Paths|\) 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\leq m\leq10^4\) tarjan 边双无疑不用考虑,于是就可以边双缩点成一棵树 令现在要连的边为 \((u,\ v)\) ,点集 \(\{u->lca(u,\ v),\ v->lca(u,\ v)\}\) 将会变为一个新的点双,可以将他们看为一个新的点 可以贪心地连

冗余路径 Redundant Paths e-DCC缩点

冗余路径 Redundant Paths 题目传送 sol: 如果两点间存在至少两条不重复的路径,这说明他们两点在同一个边双连通分量(不存在割边). 那么可以进行e-DCC的缩点,得到一棵树. 对于这棵树广泛意义上的叶子节点(度数为1)而言,都还至少需要一条边连向他. 那么可以贪心的一次连两个叶子节点,答案显然就是\(cnt+1>>1\). #include<bits/stdc++.h> #define IL inline #define RG register #define D

洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to t

luogu题解 P2860[USACO冗余路径Redundant Paths] 缩点+桥

题目链接 https://www.luogu.org/problemnew/show/P2860 https://www.lydsy.com/JudgeOnline/problem.php?id=1718 分析 首先这题目的意思就是让任意两点之间至少有两条没有重复道路的路径,很显然,如果这个图不存在桥,就一定满足上述条件. 于是我们就是要求使这个图不存在桥需要连接的最小边数 如果把桥从图中去掉,很显然剩余的联通块中任意两点之间至少有两条没有重复道路的路径(当然也可能不是联通块而是孤立的点),对答

[USACO06JAN]冗余路径Redundant Paths(缩点)

为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分离的路径,这样她们就有多一些选择. 每对草场之间已经有至少一条路径.给出所有R(F-1≤R≤10000)条双向路的描述,每条路连接了两个不同的草场,请计算最少的新建道路的数量, 路径由若干道路首尾相连而成.两条路径相互分离,是指两条路径没有一条重合的道路.但是,两条分离的路径上可以有一些相同的草场.

LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> using namespace std; const int MAXN = 5005; const int MAXM = 10005; inline i

[USACO06JAN]冗余路径Redundant Paths 无向图tarjan缩点

如题,缩完点后数一下有几个入度为1的scc,+1再/2即可. 教训:加一个cntf处理重边!否则重边会被认为是同一条. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 struct stack{ 9 vector<int> v;

BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 5009; struct edge { int to; b

1718: [Usaco2006 Jan] Redundant Paths 分离的路径

1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MB链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1718 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to an