2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test

Posted acjiumeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test相关的知识,希望对你有一定的参考价值。

题意:找三条同起点同终点的不相交的路径
题解:用tarjan的思想,记录两个low表示最小和次小的dfs序,以及最小和次小的位置,如果次小的dfs序比dfn小,那么说明有两条返祖边,那么就是满足条件的答案

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
//#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=50000+10,inf=0x3f3f3f3f;

vi v[N],road;
int dfn[N],low[N][2],fa[N];
pii pos[N][2];
int ind;
bool ok;
void pr()
{
    printf("%d",road.size());
    for(int i=0;i<road.size();i++)printf(" %d",road[i]);puts("");
    road.clear();
}
void tarjan(int u,int f)
{
    if(ok)return ;
    dfn[u]=low[u][0]=low[u][1]=++ind;
    for(int i=0;i<v[u].size();i++)
    {
        int x=v[u][i];
        if(x==f)continue;
        if(!dfn[x])
        {
            fa[x]=u;
            tarjan(x,u);
            if(low[x][1]<low[u][0])
            {
                low[u][1]=low[u][0];
                pos[u][1]=pos[u][0];
                low[u][0]=low[x][1];
                pos[u][0]=pos[x][1];
            }
            else if(low[x][1]<low[u][1])
            {
                low[u][1]=low[x][1];
                pos[u][1]=pos[x][1];
            }
            if(low[x][0]<low[u][0])
            {
                low[u][1]=low[u][0];
                pos[u][1]=pos[u][0];
                low[u][0]=low[x][0];
                pos[u][0]=pos[x][0];
            }
            else if(low[x][0]<low[u][1])
            {
                low[u][1]=low[x][0];
                pos[u][1]=pos[x][0];
            }
        }
        else if(dfn[x]<dfn[u])
        {
            if(dfn[x]<low[u][0])
            {
                low[u][1]=low[u][0];
                pos[u][1]=pos[u][0];
                low[u][0]=dfn[x];
                pos[u][0]=mp(u,x);
            }
            else if(dfn[x]<low[u][1])
            {
                low[u][1]=dfn[x];
                pos[u][1]=mp(u,x);
            }
        }
    }
    if(low[u][1]<dfn[u]&&!ok)
    {
        ok=1;
        printf("%d %d
",u,pos[u][1].se);
        for(int now=u;now!=pos[u][1].se;now=fa[now])road.pb(now);
        road.pb(pos[u][1].se);
        pr();
        road.pb(pos[u][1].se);
        for(int now=pos[u][1].fi;now!=u;now=fa[now])road.pb(now);
        road.pb(u);
        reverse(road.begin(),road.end());
        pr();
        for(int now=pos[u][1].se;now!=pos[u][0].se;now=fa[now])road.pb(now);
        road.pb(pos[u][0].se);
        for(int now=pos[u][0].fi;now!=u;now=fa[now])road.pb(now);
        road.pb(u);
        reverse(road.begin(),road.end());
        pr();
//        for(int i=1;i<=8;i++)printf("%d %d
",i,fa[i]);
//        printf("%d %d %d %d %d %d %d %d---
",u,dfn[u],low[u][0],pos[u][0].fi,pos[u][0].se,low[u][1],pos[u][1].fi,pos[u][1].se);
        return ;
    }
}
int main()
{
    freopen("grand.in","r",stdin);
    freopen("grand.out","w",stdout);
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            dfn[i]=low[i][0]=low[i][1]=fa[i]=0;
            v[i].clear();
        }
        ind=0;
        for(int i=0,x,y;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            v[x].pb(y),v[y].pb(x);
        }
        ok=0;
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                tarjan(i,-1);
        if(!ok)puts("-1");
    }
    return 0;
}
/********************
1
8 9
1 2
1 3
 4
2 5
3 6
4 7
5 8
6 8
7 8
********************/

以上是关于2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test的主要内容,如果未能解决你的问题,请参考以下文章

2017-2018 ACM-ICPC, NEERC, Northern Subregional ContestG - Grand Test

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)题解+补题

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)题解+补题

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)题解+补题

2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练

D. Dog Show 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mir