Codeforces Round #199 (Div. 2) E. Xenia and Tree
2013-09-09 16:12
295 查看
题目链接
2了,差点就A了。。。这题真心不难,开始想的就是暴力spfa就可以,直接来了一次询问,就来一次的那种,TLE了,想了想,存到栈里会更快,交又TLE了。。无奈C又被cha了,我忙着看C去了。。。这题,是我一个地方写错了。。top = 0的时候会死循环吗?貌似不会把,反正我加了这个判断,就A了,可能优化了一下把。
2了,差点就A了。。。这题真心不难,开始想的就是暴力spfa就可以,直接来了一次询问,就来一次的那种,TLE了,想了想,存到栈里会更快,交又TLE了。。无奈C又被cha了,我忙着看C去了。。。这题,是我一个地方写错了。。top = 0的时候会死循环吗?貌似不会把,反正我加了这个判断,就A了,可能优化了一下把。
#include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <cmath> #include <queue> using namespace std; #define INF 1000000 int first[200001]; int dis[200001]; int in[200001]; int s[200001]; int n,t,top; struct node { int u,v,next; }edge[300001]; void CL() { t = 0; memset(first,-1,sizeof(first)); } void add(int u,int v) { edge[t].u = u; edge[t].v = v; edge[t].next = first[u]; first[u] = t ++; } void spfa() { int i,u,v; for(i = 1;i <= n;i ++) in[i] = 0; queue<int>que; for(i = 0;i < top;i ++) { in[s[i]] = 1; dis[s[i]] = 0; que.push(s[i]); } while(!que.empty()) { u = que.front(); que.pop(); in[u] = 0; for(i = first[u];i != -1;i = edge[i].next) { v = edge[i].v; if(dis[v] > dis[u] + 1) { dis[v] = dis[u] + 1; if(!in[v]) { in[v] = 1; que.push(v); } } } } return ; } int main() { int m,i,u,v; CL(); scanf("%d%d",&n,&m); for(i = 1;i < n;i ++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } for(i = 1;i <= n;i ++) dis[i] = INF; top = 1; s[0] = 1; spfa(); top = 0; for(i = 1;i <= m;i ++) { scanf("%d%d",&u,&v); if(u == 1) { s[top ++] = v; } else { if(top != 0)//这里 spfa(); top = 0; printf("%d\n",dis[v]); } } return 0; }
相关文章推荐
- Codeforces Round #199 (Div. 2) E. Xenia and Tree(分块)
- Codeforces Round #199 (Div. 2) E. Xenia and Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #395 (Div. 2)Timofey and a tree
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
- Codeforces Round #381 (Div. 1) B Alyona and a tree 树状数组
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
- Codeforces Round #395 (Div. 1) A. Timofey and a tree(深搜)
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree
- Codeforces Round #199 (Div. 2) -- A. Xenia and Divisors (思路)
- Codeforces Round #263 (Div. 1 - B) Appleman and Tree
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs+二分+线段树延迟操作、树形化线性
- 【VK Cup 2016 - Round 1 (Div 2 Edition)C】【构造】Bear and Forgotten Tree 3 构造一棵树直径为d且点1的深度为h
- 【CodeForces】E. Xenia and Tree(分块 + LCA)
- Codeforces Round #395 (Div. 2) C. Timofey and a tree(树的基础应用)
- Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)
- Codeforces Round #358 (Div. 2) Alyona and the Tree