【NOIP2010 模拟赛】晨跑路径

来源:互联网 发布:java return this 编辑:程序博客网 时间:2024/04/30 08:31

【NOIP2010 模拟赛】晨跑路径

Description

天天到部队,教官要求他们每天早上晨跑,从A农场跑到B农场。从A农场到B农场中有n-2个路口,分别标上号A农场为1号,B农场为n号,路口分别为2..n-1号,从A农场到B农场有很多条路径可以到达,而教官发现有的路口是必须经过的,即每一条路径都经过的路口,教官要把他们记录下来,这样教官就可以先到达那个路口,观察天天有没有偷懒,而你的任务就是找所有必经路口。

Input

第一行两个用空格隔开的整数n(3<=n<=2000)和e(1<=e<=8000)。

接下来从第2行到第e+1行,每行两个用空格隔开的整数p和q,表示路口p和q之间连通。

输入数据保证必经路口一定存在,并且每个路口都和A农场,B农场相连通。

Output

第一行一个整数m,表示必经路口的数目。

第二行按从小到大的顺序依次输出每个必经路口的编号,每两个数之间用空格隔开。

注意:不包括起点和终点

Sample Input

6 6

1 2

2 4

2 3

3 5

4 5

5 6

Sample Output

2

2 5

Source

搜索 ,图论

Solution

就是求割点的个数,可以用tarjan(无奈不会啊),于是就用了很暴力的暴力

直接枚举每一个点,check将其断开后是否可以到达终点,能到达即不是割点,不能到达即满足情况,将其记录存入数组,最后一次性输出即可

Code

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <map>#include <vector>#include <queue>#define L 5000#define LL long longusing namespace std;struct node {  int nxt, to;} x[L * 3];int n, e, a, b, cnt, head[L], ans[L], num;bool vis[L], bj;inline void add(int from, int to) {  x[++cnt].nxt = head[from];  x[cnt].to = to;  head[from] = cnt;}inline void dfs(int a) {  for (int i = head[a]; i; i = x[i].nxt)    if (!vis[x[i].to]) {      if (x[i].to == n) {bj = false; return ;}      vis[x[i].to] = true;      dfs(x[i].to);    }}int main() {  freopen("running.in", "r", stdin);  freopen("running.out", "w", stdout);  scanf("%d %d", &n, &e);  for (int i = 1; i <= e; ++i) {    scanf("%d %d", &a, &b);    add(a, b), add(b, a);  }  for (int i = 2; i < n; ++i) {    bj = true;    memset(vis, false, sizeof(vis));    vis[i] = true;    dfs(1);    if (bj) ans[++num] = i;  }  printf("%d\n", num);  for (int i = 1; i <= num; ++i) printf("%d ", ans[i]);  return 0;}

Summary

考试的时候想了很暴力的暴力:找到方案总数,以及所用方案中每个点被经过的次数,如果经过的次数等与方案总数,那么这个点就是必须经过的点,结果只有10分

后来开始求割点,结果数组开小了WA了一次,把数组开大就A了

0 0
原创粉丝点击