zoj3509 Island Communication

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 9
I 1 2
I 2 3
Q 1 3
I 2 4
I 1 3
I 4 5
D 1 2
D 2 3
Q 1 5
5 10
I 1 2
I 2 3
Q 1 3
I 1 3
I 2 4
I 4 5
D 1 2
D 2 3
Q 1 5
Q 2 4
3 6
I 1 2
I 2 3
I 3 1
D 1 2
D 2 3
Q 1 2


*/


 

你可能感兴趣的:(zoj3509 Island Communication)