Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563
Test instructions: In the Milky Way, the planet is invaded by monsters, and a planet needs to ask the planet for help in a planet that is stronger than his defenses and connected to him. However, some of these edges can be compromised.
Thinking: This is a reverse thinking, as long as you know the reverse operation, carefully think, you will find that the sequence of operations is to delete the side, the reverse operation is the addition of edges, and the addition of one side of the operation is and check the typical characteristics of the set. In the end, you just have to deal with the order of the merges.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <vector>
#include <algorithm> using namespace std;
struct edge{int A, b;
BOOL operator < (const edge &t) const{if (A!=T.A) return a<t.a;
Return b<t.b;
}}e[20010];
Map<edge,int> MP;
Vector<int> ans;
int n,m,q,p[10010];
int d[50010],vis[50010];
int pre[10010];
Char s[10],op[50010];
int find (int x) {int t=x;
while (T!=pre[t]) t=pre[t];
while (x!=t) pre[x]=t,x=pre[x];
return t;
} void join (int k) {int x=e[k].a,y=e[k].b;
int Fx=find (x), Fy=find (y); if ((p[fx]<p[fy) | | |
(P[fx]==p[fy]) &&fx>fy) swap (FX,FY);//merge pre[fy]=fx as required;
} int query (int x) {int fx=find (x);
if (p[fx]<=p[x]) return-1;
return FX;
} int main () {//freopen ("D:\\in.txt", "R", stdin);
int kase=0;
while (cin>>n) {if (kase++) cout<<endl;
memset (vis,0,sizeof (VIS)); FoR (int i=0;i<=n;i++) pre[i]=i;
Mp.clear ();
for (int i=0;i<n;i++) scanf ("%d", &p[i]);
cin>>m;
int A, B; for (int i=0;i<m;i++) {scanf ("%d%d", &a,&b), if (a>b) swap (b);//Note sort e[i]= (edge) {b}
;
Mp[e[i]]=i;
} cin>>q;
for (int i=0;i<q;i++) {//Record Q related inquiry operation scanf ("%s", s); op[i]=s[0];
if (s[0]== ' d ') {scanf ("%d%d", &a,&b);
if (a>b) swap (A, B),//corresponds to the previous sort d[i]=mp[(edge) {B}];
vis[d[i]]=1;//records its side ordinal}else{scanf ("%d", &a); d[i]=a;//records the object to be queried}} for (int i=0;i<m;i++) if (!vis[i]) join (i);//Create and check the set without deleting the edges first ans.cl
Ear ();
for (int i=q-1;i>=0;i--) {//Counter-direction to perform an inquiry operation if (op[i]== ' d ') join (D[i]);
else Ans.push_back (query (D[i]));
} for (int i=ans.size () -1;i>=0;i--) printf ("%d\n", Ans[i]);
} return 0; }