zoj3509 Island Communication
来源:互联网 发布:风机选型软件制作 编辑:程序博客网 时间:2024/05/17 01:02
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4352
有题解给的是动态树 http://hi.baidu.com/xpycc/item/0e5b0feb827e203a86d9de92
自己一顿乱搞,双向链表 。
#include <cstdio>#include <iostream>#include <cstring>#include <map>#include <string>#include <algorithm>#include <map>#include <queue>using namespace std;const int inf = 0x7fffffff;const int maxm=50000+123;const int maxn=555;struct Edge{ int v, next, pre;}edge[maxm<<2];int cnt, head[maxn];int Map[maxn][maxn];int D[maxn][maxn];///char ops[maxm];int x[maxm], y[maxm];void addedge(int u, int v){ edge[cnt].v=v; edge[cnt].next=head[u]; edge[cnt].pre=-1; if(~head[u])edge[head[u]].pre=cnt; Map[u][v]=cnt; head[u]=cnt++; edge[cnt].v=u; edge[cnt].next=head[v]; edge[cnt].pre=-1; if(~head[v])edge[head[v]].pre=cnt; Map[v][u]=cnt; head[v]=cnt++;}void deledge(int u, int v){ int p1=Map[u][v], p2=Map[v][u]; if(p1 == -1 || p2 == -1) return;///边已经不存在了 Map[u][v]=Map[v][u]=-1; if(~edge[p1].pre)edge[edge[p1].pre].next=edge[p1].next; else head[u]=edge[p1].next; if(~edge[p1].next)edge[edge[p1].next].pre=edge[p1].pre; if(~edge[p2].pre)edge[edge[p2].pre].next=edge[p2].next; else head[v]=edge[p2].next; if(~edge[p2].next)edge[edge[p2].next].pre=edge[p2].pre;}void init(int n){ cnt=0; memset (head, -1, sizeof(int)*(n+5)); memset (Map, -1, sizeof(Map)); for (int i=0; i<=n; ++i) for (int j=0; j<=n; ++j) D[i][j]=inf;}void debug(int n){ for (int i=1; i<=n; ++i) { printf("%d:", i); for (int p=head[i]; ~p; p=edge[p].next) { printf("->v=%d", edge[p].v); } puts(""); }}bool dfs(int u, int fa, int &dm, const int &to){ //printf("%d %d\n", u, fa); if(u==to)return true; for (int p=head[u]; ~p; p=edge[p].next) { if(edge[p].v==fa)continue; if(dfs(edge[p].v, u, dm, to)) { dm=min(dm, D[u][edge[p].v]); return true; } } return false;}int main (){ int n, m; int T=0; while (~scanf("%d%d", &n, &m)) { init(n); if(T)puts(""); printf("Case %d:\n", ++T); for (int i=0; i<m; ++i) { scanf("%s %d %d", ops+i, x+i, y+i); if(ops[i]=='D') { D[x[i]][y[i]]=D[y[i]][x[i]]=i; } } for (int i=0; i<m; ++i) { if(ops[i]=='I') { int dm=inf; if(dfs(x[i], -1, dm, y[i])) { if(dm<D[x[i]][y[i]]) { addedge(x[i], y[i]); deledge(x[dm], y[dm]); } } else addedge(x[i], y[i]); } if(ops[i]=='D' ) { deledge(x[i], y[i]); } if(ops[i]=='Q') { int dm=inf; puts(dfs(x[i], -1, dm, y[i])?"YES":"NO"); }// printf("op: %d %c %d %d\n", i, ops[i], x[i], y[i]);// debug(n); } } return 0;}/*5 9I 1 2I 2 3Q 1 3I 2 4I 1 3I 4 5D 1 2D 2 3Q 1 55 10I 1 2I 2 3Q 1 3I 1 3I 2 4I 4 5D 1 2D 2 3Q 1 5Q 2 43 6I 1 2I 2 3I 3 1D 1 2D 2 3Q 1 2*/
- zoj3509 Island Communication
- zoj 3500 island communication
- zoj 3509 Island Communication
- Island Communication -遍历邻接表
- Communication
- Communication
- Communication
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- FlyWeight模式
- SCM介绍
- VS2005不能进入断点调试, 注册表问题
- 作为Web开发人员,我为什么喜欢Google Chrome浏览器
- 重复注册按钮事件导致事件处理异常
- zoj3509 Island Communication
- Framework/base下添加自定义模块的编译
- 如何在C语言中调用shell命令
- GridControl详解(七)事件
- 上班了。
- 【Win32多线程】创建线程、获取线程执行状态、退出线程、错误处理
- C#中委托和事件
- 这里什么都没有!
- 【Kris专题】Android 获取、设置铃声、音量、静音、扬声器