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

为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分离的路径,这样她们就有多一些选择.

每对草场之间已经有至少一条路径.给出所有R(F-1≤R≤10000)条双向路的描述,每条路连接了两个不同的草场,请计算最少的新建道路的数量, 路径由若干道路首尾相连而成.两条路径相互分离,是指两条路径没有一条重合的道路.但是,两条分离的路径上可以有一些相同的草场. 对于同一对草场之间,可能已经有两条不同的道路,你也可以在它们之间再建一条道路,作为另一条不同的道路.



将所有的点弄到一个环内

先缩点,在找到那些只有一个度的点

那就是需要连的点cnt个

ans=(cnt+1)>>1

#include<bits/stdc++.h>
#define re return
#define ll long long
#define inc(i,l,r) for(int i=l;i<=r;++i)
using namespace std;
template<typename T>inline void rd(T&x)
{
    char c;bool f=0;
    while((c=getchar())<‘0‘||c>‘9‘)if(c==‘-‘)f=1;
    x=c^48;
    while((c=getchar())>=‘0‘&&c<=‘9‘)x=x*10+(c^48);
    if(f)x=-x;
}

const int maxn=5005,maxm=10005;
int n,m,k=1,tot,col,hd[maxn];
int belong[maxn],low[maxn],dfn[maxn],d[maxn];
struct node{
    int to,nt;
}e[maxm<<1];

inline void add(int x,int y)
{
    e[++k].to=y;e[k].nt=hd[x];hd[x]=k;
    e[++k].to=x;e[k].nt=hd[y];hd[y]=k;
}

stack<int>s;

inline void dfs(int x,int fan)
{

    dfn[x]=low[x]=++tot;
    s.push(x);
    for(int i=hd[x];i;i=e[i].nt)
    {
        int v=e[i].to;
        if(fan==(i^1))continue;
        //13->16
        //16->13
        //缩点引发的悲剧
        if(!dfn[v])
        {
            dfs(v,i);
            low[x]=min(low[x],low[v]);
        }
        else low[x]=min(low[x],dfn[v]);
    }

    if(dfn[x]==low[x])
    {
        int v=-1;
        ++col;
        while(v!=x)
        {
            v=s.top();
            s.pop();
            belong[v]=col;
        }
    }
}

int main()
{
//    freopen("in.txt","r",stdin);
    int x,y;
    rd(n),rd(m);
    inc(i,1,m)
    {
        rd(x),rd(y);
        add(x,y);
    }

    inc(i,1,n)
    if(!dfn[i])dfs(i,0);

    int ans=1;
    for(int i=2;i<=k;i+=2)
    {
        x=belong[e[i].to];y=belong[e[i^1].to];
        if(x!=y)
        {
            ++d[x];
            ++d[y];
        }
    }

    inc(i,1,col)
    if(d[i]==1)
        ++ans;

    printf("%d",ans>>1);
    re 0;
}

原文地址:https://www.cnblogs.com/lsyyy/p/11420703.html

时间: 2024-07-29 19:10:00

[USACO06JAN]冗余路径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)\}\) 将会变为一个新的点双,可以将他们看为一个新的点 可以贪心地连

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;

洛谷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 分析 首先这题目的意思就是让任意两点之间至少有两条没有重复道路的路径,很显然,如果这个图不存在桥,就一定满足上述条件. 于是我们就是要求使这个图不存在桥需要连接的最小边数 如果把桥从图中去掉,很显然剩余的联通块中任意两点之间至少有两条没有重复道路的路径(当然也可能不是联通块而是孤立的点),对答

冗余路径 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

冗余路径Redundant Paths

题意翻译 题目描述 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每两个城镇之间至多只有一条直接连接的道路.人们可以从任意一个城镇直接或间接到达另一个城镇. 每个城镇都有一个公民,他们被孤独所困扰.事实证明,每个公民都想拜访其他所有公民一次(在主人所在的城镇).所以,一共会有n*(n-1)次拜访. 不幸的是,一个程序员总罢工正在进行中,那些程序员迫切要求购买某个软件. 作为抗议行动,程序员们计划封锁一些城镇,阻

【连通图|边双连通+缩点】POJ-3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K       Description 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 T

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro