zoj 3261 Connection in Galaxy War


Connection in Galaxy War
Time Limit:3 Seconds    
メモリLimit:32768 KB
Inordeder to streengthen the defense abiility、many stars in galaxy allied together and built manbidiinininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininststststststststststininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininothers.
In the galaxy,the stars are numberd from 0 to N-1 and their power marked by a non-negative integer pi.When the star A wanted to seek help,it would send the message to the star with the ladtwaretchthis star shound be more powerful than the star A.If there were more than one star which had the same larget power,then the one with the smalest serial number.And therefore,sometimes star couldfich's.
Gven the information of the war and the queries about some particular stars,for each query,please find out whether this star could sek another star for help and which stauld chorsen.
Input
The re re no more than 20 cases.Process to the end of file.
For each cases、the first line contains an integer N(1==N==10000)、which is the number of stars.The second line contains N integers p 0、p 1、…、p-1(0==pi==100000)は、representinesenting the the of the stareline.that is the number of tunnel s built before the war.The n M lineas follows.Each line has two integers a,b(0<=a、=N-1,a==b),which means star a and star has has has a connectionectionese.Icontennect.
In the(M+2)-th line is an integer Q(0<=Q==50000)which is the number of the information and queries.In the follwing Q lines,each line will be written in one of next two formas.
「destroy a b」-the connection between star a and star b was destroyed by the monters.It's garanted that the connection between star a and star b was available before the mosters'attck.
「query a」-star a wanted to know which star it shound turn to for help
The e e e e e e is a blank line between consecutive cases.
Output
For each query in the input、if there isのstar that star a can turn to for help、then output「-1」;other wise,output the serial number of the chorsen star.
Print a blank line between consecutive cases.
Sample Input
2
10 20
1
0 1
5
query 0
query 1
destroy 0 1
query 0
query 1
Sample Output
1
-1
-1
-1
はい、今週はデータ構造週間です.データ構造の問題で、一年間ノックしていないという意味です.今日は手動でノックできるなんて不思議です.
この問題は簡単です.一つの宇宙ネットワーク、nつの星の中に戦闘力piがあり、各惑星の間にmの道があります.そして戦闘が悲惨なため、いくつかの道が戦闘中に悲劇になりました.彼より戦闘力が大きい上に、最大の援軍はどの星ですか?招待できる星の戦闘力が彼より大きいなら、出力-1.
えっと、この問題は複雑に見えますが、後から調べると明らかに個で、ルートノードは最大の戦闘力の星を保存しています.destroyに会ったら接続ノードと同じです.最初の図は元の図を調べた後で、destroyの分子図がまだありません.
コード
#include <stdio.h>
#include <vector>
using namespace std;

typedef struct
{
    int v;
    bool del;
}edge;

typedef struct
{
    int ord;
    int x,y;
}order;

order a[50005];
int p[10005];
vector <edge> q[10005];
char str[10];
int ans[50005];
int fn[10005];

int Find(int t)
{
    if (fn[t]==t) return t;
    fn[t]=Find(fn[t]);
    return fn[t];
}

void Comb(int x,int y)
{
    if (p[x]>p[y] || (p[x]==p[y] && x<y))
    {
        fn[y]=x;
    }
    else
    {
        fn[x]=y;
    }
}

void Union(int x,int y)
{
    int t,s;
    t=Find(x);
    s=Find(y);
    if (t==s) return;
    Comb(t,s);
}

int main()
{
    int i,j,n,f,m,x,y,Q,up;
    edge tag;
    f=0;
    while(scanf("%d",&n)!=EOF)
    {
        if (f==1) printf("
"); f=1; for (i=0;i<n;i++) { scanf("%d",&p[i]); q[i].clear(); } scanf("%d",&m); tag.del=0; for (i=0;i<m;i++) { scanf("%d%d",&x,&y); tag.v=y; q[x].push_back(tag); tag.v=x; q[y].push_back(tag); } scanf("%d",&Q); for (i=0;i<Q;i++) { scanf("%s",str); if (str[0]=='d') { scanf("%d%d",&x,&y); a[i].ord=1; a[i].x=x; a[i].y=y; for (j=0;j<q[x].size();j++) { if (q[x][j].v==y) break; } q[x][j].del=1; for (j=0;j<q[y].size();j++) { if (q[y][j].v==x) break; } q[y][j].del=1; } else { scanf("%d",&x); a[i].ord=2; a[i].x=x; } } for (i=0;i<n;i++) { fn[i]=i; } for (i=0;i<n;i++) { for (j=0;j<q[i].size();j++) { if (q[i][j].del==0) Union(i,q[i][j].v); } } up=0; for (i=Q-1;i>=0;i--) { if (a[i].ord==1) { Union(a[i].x,a[i].y); } else { x=Find(a[i].x); if (p[x]!=p[a[i].x]) { ans[up++]=x; } else { ans[up++]=-1; } } } for (i=up-1;i>=0;i--) { printf("%d
",ans[i]); } } return 0; }