USACO Section 4.3 Street Race(图的连通性+枚举)

虽说是IOI‘95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通;for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚举,也是用dfs判断。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>

#define rep(i,r) for(int i=0;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x))

using namespace std;

const int maxn=50+5;

int n=0;
int vis[maxn];

vector<int> g[maxn];

void read() {
    for(;;) {
        int t,pd=0;
        g[n].clear();
        while(scanf("%d",&t) && t!=-2) {
            if(t==-1) { pd=1; break; }
            g[n].push_back(t);
        }
        if(pd) break;
        n++;
    }
}

int dfs(int i) {
    vis[i]=1;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(t==n-1) return 1;
        if(vis[t]) continue;
        if(dfs(t)) return 1;
    }
    return 0;
}

void DFS(int i) {
    vis[i]=1;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(vis[t]) continue;
        DFS(t);
    }
}

int dfsJudge(int i) {
    vis[i]=2;
    rep(j,g[i].size()) {
        int t=g[i][j];
        if(vis[t]==1) return 1;
        if(vis[t]) continue;
        if(dfsJudge(t)) return 1;
    }
    return 0;
}

void work() {

    vector<int> ans;
    ans.clear();
    for(int i=1;i<n-1;i++) {
        clr(vis,0);
        vis[i]=1;
        if(!dfs(0)) ans.push_back(i);
    }

    cout<<ans.size();
    rep(i,ans.size()) cout<<‘ ‘<<ans[i];
    cout<<endl;

    vector<int> ans2;
    ans2.clear();
    rep(i,ans.size()) {
         clr(vis,0);
         vis[ans[i]]=2;
         DFS(0);
         if(!dfsJudge(ans[i])) ans2.push_back(ans[i]);
    }

    printf("%d",ans2.size());
    rep(i,ans2.size()) printf(" %d",ans2[i]);
    cout<<endl;
}

int main()
{
    freopen("race3.in","r",stdin);
    freopen("race3.out","w",stdout);

    read();

    work();

    return 0;
}

Street Race
IOI‘95

Figure 1 gives an example of a course for a street race. You see some points, labeled from 0 to N (here, N=9), and some arrows connecting them. Point 0 is the start of the race; point N is the finish. The arrows represent one-way streets. The participants of the race move from point to point via the streets, in the direction of the arrows only. At each point, a participant may choose any outgoing arrow.

 
Figure 1: A street course with 10 points

A well-formed course has the following properties:

  • Every point in the course can be reached from the start.
  • The finish can be reached from each point in the course.
  • The finish has no outgoing arrows.

A participant does not have to visit every point of the course to reach the finish. Some points, however, are unavoidable. In the example, these are points 0, 3, 6, and 9. Given a well-formed course, your program must determine the set of unavoidable points that all participants have to visit, excluding start and finish.

Suppose the race has to be held on two consecutive days. For that purpose the course has to be split into two courses, one for each day. On the first day, the start is at point 0 and the finish at some `splitting point‘. On the second day, the start is at this splitting point and the finish is at point N. Given a well-formed course, your program must also determine the set of splitting points. A point S is a splitting point for the well-formed course C if S differs from the star t and the finish of C, and the course can be split into two well-formed courses that (1) have no common arrows and (2) have S as their only common point, with S appearing as the finish of one and the start of the other. In the example, only point 3 is a splitting point.

PROGRAM NAME: race3

INPUT FORMAT

The input file contains a well-formed course with at most 50 points and at most 100 arrows. There are N+2 lines in the file. The first N+1 lines contain the endpoints of the arrows that leave from the points 0 through N respectively. Each of these lines ends with the number -2. The last line contains only the number -1.

SAMPLE INPUT (file race3.in)

1 2 -2
3 -2
3 -2
5 4 -2
6 4 -2
6 -2
7 8 -2
9 -2
5 9 -2
-2
-1

OUTPUT FORMAT

Your program should write two lines. The first line should contain the number of unavoidable points in the input course, followed by the labels of these points, in ascending order. The second line should contain the number of splitting points of the input course, followed by the labels of all these points, in ascending order.

SAMPLE OUTPUT (file race3.out)

2 3 6
1 3
时间: 2024-12-14 12:36:43

USACO Section 4.3 Street Race(图的连通性+枚举)的相关文章

USACO Section 2.1 Healthy Holsteins

/* ID: lucien23 PROG: holstein LANG: C++ */ #include <iostream> #include <fstream> #include <vector> using namespace std; bool compFun(int x, int y) { int temp, i = 0; while (true) { temp = 1 << i; if (temp&x > temp&y) {

USACO Section 2.2 Party Lamps

/* ID: lucien23 PROG: lamps LANG: C++ */ /* * 此题的技巧之处就是需要注意到任何button只要按下2的倍数次就相当于没有按 * 所以其实只需要考虑4个按钮,每个按钮是否被有效按下过一次就好 * 直接使用枚举法,一共只有2^4=16种情况 * 对于每种情况需要知道被按下的有效次数(也就是被按下过的按钮数),必须满足 * (C-有效次数)%2=0才行,这样其他次数才能视为无效 * 然后验证各种情况是否符合要求,将符合要求的情况按序输出即可 */ #inc

USACO Section 2.2 Runaround Numbers

/* ID: lucien23 PROG: runround LANG: C++ */ #include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { ifstream infile("runround.in"); ofstream outfile("runround.out"); if(!infile || !

USACO Section 2.2 Preface Numbering

/* ID: lucien23 PROG: preface LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <map> using namespace std; int main() { ifstream infile("preface.in"); ofstream outfile("preface.out")

POJ2513(字典树+图的连通性判断)

//用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; const int MAXN=510005; const int N=26;//26个小写英文字母 struct node{ int val;//存放字符串的hash值 node* next[N]; }; node memory[MAXN]; int ant; node* root; node* create_tree() {

49. 蛤蟆的数据结构笔记之四十九图的连通性问题

49. 蛤蟆的数据结构笔记之四十九图的连通性问题 本篇名言:"我们不得不饮食.睡眠.游惰.恋爱,也就是说,我们不得不接触生活中最甜蜜的事情:不过我们必须不屈服于这些事物 .....--约里奥?居里"     此篇就作为数据结构入门笔记的最后一篇吧. 欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/47135259 设图G=(V,E)是一个无向图,G的一个连通分支是一个最大的连通子图,即一个连通分支是不包含在任何更大的

图的连通性问题的小结 (双连通、2-SAT)

图的连通性问题包括: 1.强连通分量. 2.最小点基和最小权点基. 3.双连通. 4.全局最小割. 5.2-SAT 一.强连通分量 强连通分量很少单独出题,一般都是把求强连通分量作为缩点工具. 有三种算法: 1.Kosaraju算法.对原图和反图分别进行一次深度优先搜索. 2.Tarjan算法.用了时间戳. 3.Garbow算法.与Tarjan算法是同一思想,但更精妙. 三种算法的模版我已经贴过了  http://www.cnblogs.com/Potato-lover/p/3956604.ht

POj 1386 Play on words 欧拉回路/通路,图的连通性判断

题目链接: #include<iostream> #include<cstdio> #include<cstring> #define M 28 using namespace std; int fa[M]; int Find(int x) { return x==fa[x]?x:fa[x]=Find(fa[x]); } int main() { int T,n; char str[1005]; int in_degree[M],out_degree[M]; int v

10.4图的连通性(Connectivity)

10.4图的连通性(Connectivity) 如果存在一条路径,使得从u到v,那么顶点u和v就是连通的 如果能存在一条路径从u到u,那么这样的路径称为自环(circuit) 路径可以视作穿过点(through the vertices),也可视为遍历边(traverses the edges) 如果这条路径穿过的边都是一次的(无重复穿过同一条边),那么我们称其为简单路径 图连通 针对于无向图而言,如果任意两个顶点都能够连通,那么称这个图为连通图 定理: 无向连通图的每对顶点都存在简单路径(si