题目链接:http://codeforces.com/problemset/problem/131/D

思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上顶点的距离不计)。

一开始我是直接用Tarjan求的无向图的双连通分量,然后标记连通分量上的点(如果某一个连通分量上的顶点的个数大于1,那么就是环了,其余的都只有一个点),然后即使重新建图,spfa求最短路径。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (3000 + 300);
int dfn[MAX_N], low[MAX_N], cnt, N, _count, color[MAX_N];
int st, dist[MAX_N];
bool mark[MAX_N];
vector<int > g[MAX_N], reg[MAX_N];
stack<int > S; void Tarjan(int u, int father)
{
low[u] = dfn[u] = ++cnt;
S.push(u);
mark[u] = true;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (father == v) continue;
if (dfn[v] == 0) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if (mark[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
int x, num = 0;
++_count;
do {
x = S.top();
S.pop();
mark[x] = false;
color[x] = _count;
++num;
} while (x != u);
if (num > 1) st = _count;
}
} void spfa(int st)
{
queue<int > que;
memset(dist, 0x3f, sizeof(dist));
memset(mark, false, sizeof(mark));
dist[st] = 0;
que.push(st);
while (!que.empty()) {
int u = que.front();
que.pop();
REP(i, 0, (int)reg[u].size()) {
int v = reg[u][i];
if (dist[u] + 1 < dist[v]) {
dist[v] = dist[u] + 1;
if (!mark[v]) {
mark[v] = true; que.push(v);
}
}
}
}
} int main()
{
while (cin >> N) {
FOR(i, 1, N) g[i].clear(), reg[i].clear();
FOR(i, 1, N) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cnt = _count = 0;
memset(dfn, 0, sizeof(dfn));
memset(mark, false, sizeof(mark));
FOR(i, 1, N) if (!dfn[i]) Tarjan(i, -1);
FOR(u, 1, N) {
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (color[u] != color[v]) reg[color[u]].push_back(color[v]), reg[color[v]].push_back(color[u]);
}
}
spfa(st);
FOR(i, 1, N) {
cout << dist[color[i]];
if (i == N) cout << endl;
else cout << " ";
}
}
return 0;
}

后来我发现自己想的太复杂了,其实只要一遍dfs就能求出这个环上的点了,具体的做法是从某一点开始深搜,然后如果遇上之前搜过的点,那么说明形成一个环,用一个变量记录这个点,然后回退的时候判断是否遇到过这个点,如果没有遇到过,就把回退路径上的点都标记为环上的点,否则,继续回退。最后即使一遍bfs就可以求出最短路径。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (3000 + 300);
int N, flag[MAX_N], dist[MAX_N], mark[MAX_N], found, st, Ok;
vector<int > g[MAX_N]; void dfs(int u, int father)
{
mark[u] = true;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v == father) continue;
if (!mark[v]) dfs(v, u);
else { found = 1; st = v; flag[u] = 1; return; } if (found) {
if (Ok) return;
if (st == u) Ok = 1;
flag[u] = 1;
return;
}
}
} void bfs(int st)
{
queue<int > que;
memset(mark, false, sizeof(mark));
mark[st] = true;
dist[st] = 0;
que.push(st);
while (!que.empty()) {
int u = que.front();
que.pop();
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (mark[v]) continue;
mark[v] = true;
if (flag[v]) dist[v] = 0;
else dist[v] = dist[u] + 1;
que.push(v);
}
}
} int main()
{
while (cin >> N) {
FOR(i, 1, N) g[i].clear(), flag[i] = mark[i] = 0;
FOR(i, 1, N) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
found = Ok = 0;
dfs(1, 1);
bfs(st);
FOR(i, 1, N) {
cout << dist[i];
if (i == N) cout << endl;
else cout << " ";
}
}
return 0;
}

Codeforces Beta Round #95 (Div. 2) D.Subway的更多相关文章

  1. Codeforces Beta Round #95 (Div. 2) D. Subway dfs+bfs

    D. Subway A subway scheme, classic for all Berland cities is represented by a set of n stations conn ...

  2. Codeforces Beta Round #95 (Div. 2) D. Subway 边双联通+spfa

    D. Subway   A subway scheme, classic for all Berland cities is represented by a set of n stations co ...

  3. codeforces水题100道 第二十六题 Codeforces Beta Round #95 (Div. 2) A. cAPS lOCK (strings)

    题目链接:http://www.codeforces.com/problemset/problem/131/A题意:字符串大小写转换.C++代码: #include <cstdio> #i ...

  4. Codeforces Beta Round #95 (Div. 2) C. The World is a Theatre 组合数学

    C. The World is a Theatre There are n boys and m girls attending a theatre club. To set a play " ...

  5. Codeforces Beta Round #95 (Div. 2) C 组合数学

    C. The World is a Theatre time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  7. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. POJ 1190(深搜)

    http://poj.org/problem?id=1190 又有好久没做搜索的题了,没想到做一个卡了我那么久,想哭啊. 一个中文题,思路呢也就是搜索呗,一层一层往上面搜,不过这里有两个比较重要的地方 ...

  2. c++ 调用外部程序exe-ShellExecuteEx

    此方法最实用的调用exe. #include <ShellAPI.h> string file_path = s_run_dir+"\\ConsoleApplication1.e ...

  3. properties配置文件的读取和写入

    /** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...

  4. 士兵杀敌(三)_RMQ(区间最值查询)

    士兵杀敌(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...

  5. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  6. code vs1517 求一次函数解析式(数论 纯数学知识)

    1517 求一次函数解析式  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 Description 相信大家都做过练 ...

  7. 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation

    查看网址:http://www.open-open.com/lib/view/open1411443332703.html

  8. xcode报错,svn : is not a workingCopy

    解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...

  9. c语言运算符

     一.op=形式的赋值操作符 int a=0; a+=1; //等价于 a=a+1;// a*=1;  二.op=类表达式 int a=10,b=5; a/=b;//等价于a=a/b; a*=b+1; ...

  10. 有关struts2中用到 js 总结

    1.js中取Struts2中的栈里的值 var current = "${currentPage}"; 2.js 如何提交执行提交url连接 ,以及 Struts中的url如何如何 ...