zoukankan      html  css  js  c++  java
  • NOIP模拟赛 混合图

    【题目描述】

      Hzwer神犇最近又征服了一个国家,然后接下来却也遇见了一个难题。

      Hzwer的国家有n个点,m条边,而作为国王,他十分喜欢游览自己的国家。他一般会从任意一个点出发,随便找边走,沿途欣赏路上的美景。但是我们的Hzwer是一个奇怪的人,他不喜欢走到自己以前走过的地方,他的国家本来有p1条有向边,p2条无向边,由于国王奇怪的爱好,他觉得整改所有无向边,使得他们变成有向边,要求整改完以后保证他的国家不可能出现从某个地点出发顺着路走一圈又回来的情况。(注:m=p1+p2.)

      概述:给你一张混合图,要求你为无向图定向,使得图上没有环。

    【输入格式】 dizzy.in

          第一行3个整数 n,p1,p2,分别表示点数,有向边的数量,无向边的数量。

          第二行起输入p1行,每行2个整数 a,b 表示a到b有一条有向边。

          接下来输入p2行,每行2个整数 a,b 表示a和b中间有一条无向边。

    【输出格式】 dizzy.out

      对于每条无向边,我们要求按输入顺序输出你定向的结果,也就是如果你输出a b,那表示你将a和b中间的无向边定向为a->b。

      注意,也许存在很多可行的解。你只要输出其中任意一个就好。

    【样例输入】

    4 2 3

    1 2

    4 3

    1 3

    4 2

    3 2

    【样例输出】

    1 3

    4 2

    2 3

    数据范围

    对于20%的数据 n<=10 p1<=10 p2<=5

    对于30%的数据 n<=10 p1<=30 p2<=20

    对于100%的数据 n<=100000 p1<=100000 p2<=100000

    数据保证至少有一种可行解。

    黄学长的代码好像错了(是他故意留的吗。。。

    拓扑模板就抱走了

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 struct Edge
     6 {
     7     int to,next;
     8 }E[100001];
     9 int head[100001],r[100001],q[100001],s[100001],h[100001];
    10 int node=0;
    11 
    12 int n,p1,p2;
    13 
    14 void insert(int u,int v)
    15 {
    16     E[++node]=(Edge){v,head[u]};
    17     head[u]=node;
    18     r[v]++;
    19 }
    20 
    21 void topsort()
    22 {
    23     int t=1,top=0,tot=0;
    24     for(int i=1;i<=n;i++)
    25         if(!r[i])
    26         {
    27             s[++top]=i;
    28             q[i]=++tot;
    29         }
    30     while(t<=top)
    31     {
    32         int x=s[t++];
    33         for(int i=head[x];i;i=E[i].next)
    34         {
    35             r[E[i].to]--;
    36             if(!r[E[i].to])
    37             {
    38                 s[++top]=E[i].to;
    39                 q[E[i].to]=++tot;
    40             }
    41         }
    42     }
    43 }
    44 
    45 int read()
    46 {
    47     int x=0,f=1;char ch=getchar();
    48     while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
    49     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    50     return x*f;
    51 }
    52 
    53 int main()
    54 {
    55     n=read();p1=read();p2=read();
    56     for(int i=1;i<=p1;i++)
    57     {
    58         int u=read(),v=read();
    59         insert(u,v);
    60     }
    61     topsort();
    62     for(int i=1;i<=p2;i++)
    63     {
    64         int u=read(),v=read();
    65         if(q[u]<=q[v]) printf("%d %d
    ",u,v);
    66         else printf("%d %d
    ",v,u);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    Max History CodeForces
    Buy a Ticket CodeForces
    AC日记——字符串的展开 openjudge 1.7 35
    AC日记——回文子串 openjudge 1.7 34
    AC日记——判断字符串是否为回文 openjudge 1.7 33
    AC日记——行程长度编码 openjudge 1.7 32
    AC日记——字符串P型编码 openjudge 1.7 31
    AC日记——字符环 openjudge 1.7 30
    AC日记——ISBN号码 openjudge 1.7 29
    AC日记——单词倒排 1.7 28
  • 原文地址:https://www.cnblogs.com/InWILL/p/6031514.html
Copyright © 2011-2022 走看看