ZOJ3378:Attack the NEET Princess (边双连通分量)

Houraisan Kaguya (蓬萊山輝夜) is the NEET (not in education, employment or training) princess living in Eientei (永遠亭). She is an unemployed geek, who sits at home and surfs the Internet all day. But today is different, as the Hakurei Shrine Reitaisai Festival (博麗神社例大祭) will be hosted in Hakurei Shrine, Kaguya decides to go to buy some new doujin games.

Fujiwara no Mokou (藤原妹紅) detests Kaguya, and has been planing to attack her for a long time. But she nerver got the chance before because Kaguya kept staying in Eientei, under the protection of Yagokoro Eirin (八意永琳). Knowing that Kaguya will go outside today, Mokou decides to wait on certain road and attack her once she passes there. Mokou don't know which path Kaguya will choose, but she thinks that there always exits some roads where she can always meet Kaguya.

Input

There are multiple cases. Each case begins with two integers 2 ≤ n ≤ 10000 -- the number of villages and 2 ≤ m ≤ 100000 -- the number of roads. Then m lines, each contains two integers a and b (0 ≤ ab < n), indicating a road connecting village a and village b. Village 0 is Eientei and Village n-1 is Hakurei Shrine. They are always connected by roads. There may be more than one roads between two villages.

<h4< dd="">Output

Find out the roads where Mokou can always meet Kaguya. For each case, output the number of roads in the first line, then output the numbers of the roads in ascending order in the second line.

<h4< dd="">Sample Input
3 2
0 1
1 2

7 8
0 1
0 2
1 3
2 3
3 4
3 5
4 6
5 6

<h4< dd="">Sample Output
2
0 1
0

References

上海アリス幻樂団

acm_x_touhou

题意:给一个无向图,问0号点到n-1号点,哪些边是必须经过的,可能有重边。

思路:就是相当于找连接双连通分量的桥,重边需要处理,最后构造出来是一棵树,dfs一下路径就行。

# include <iostream>
# include <cstring>
# include <cstdio>
# include <vector>
# include <algorithm>
# include <stack>
# define PII pair<int,int>
# define mp make_pair
# define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
const int maxn = 1e4+30;
int pre[maxn],low[maxn],bccno[maxn],dfs_clock,bcc_cnt;
int st, ed, dad[maxn];
stack<int>S;
vector<int>ans;
vector<PII >G[maxn], V[maxn];
void dfs(int u,int fa) {
    low[u]=pre[u]=++dfs_clock;
    dad[u]=fa;
    S.push(u);
    int k=0;
    for(int i=0; i<G[u].size(); ++i)
    {
        int v=G[u][i].first;
        if(v==fa && !k)
        {
            ++k;
            continue;
        }
        if(!pre[v]) dfs(v,u);
        low[u]=min(low[u],low[v]);
    }
    if(pre[u]==low[u])
    {
        ++bcc_cnt;
        int tmp;
        do
        {
            tmp=S.top();
            S.pop();
            bccno[tmp]=bcc_cnt;
        }while(u!=tmp);
    }
}
bool dfs2(int cur, int pre)
{
    if(cur == ed) return true;
    for(int i=0; i<V[cur].size(); ++i)
    {
        int to = V[cur][i].first, id=V[cur][i].second;
        if(to == pre) continue;
        if(dfs2(to, cur))
        {
            ans.pb(id);
            return true;
        }
    }
    return false;
}
int main()
{
    int n, m, u, v;
    while(~scanf("%d%d",&n,&m))
    {
        ans.clear();
        for(int i=0; i<=n; ++i) G[i].clear();
        dfs_clock = bcc_cnt = 0;
        memset(pre, 0, sizeof(pre));
        memset(dad,0,sizeof(dad));
        memset(low, 0, sizeof(low));
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d",&u,&v);
            G[u].pb(mp(v,i));
            G[v].pb(mp(u,i));
        }
        for(int i=0; i<n; i++)
            if(!pre[i])dfs(i,i);
        for(int i=0; i<=bcc_cnt; ++i) V[i].clear();
        for(int i=0; i<n; ++i)
        {
            for(int k=0; k<G[i].size(); ++k)
            {
                int j=G[i][k].first, id=G[i][k].second;
                if(bccno[i] != bccno[j])
                    V[bccno[i]].pb(mp(bccno[j], id));
            }
        }
        st = bccno[0], ed = bccno[n-1];
        dfs2(st, -1);
        sort(ans.begin(), ans.end());
        printf("%d\n",ans.size());
        for(int i=0; i<ans.size(); ++i)
            printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');
        if(ans.size()==0) putchar('\n');
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值