C++--noip simulation Problem--Monkey __c++

Source: Internet
Author: User
Monkey
Topic Description

There are n monkeys, the first tail hanging on the tree, the remaining N-1, or by other monkeys catch, or catch the other monkeys, or both. Of course, a monkey catches up to two other monkeys, only two hands. Now give this n monkey catch and catch information.

At some point a monkey a will let go of its left or right monkey B, the following three situations occur:
1. b At first did not catch A, then B will fall;
2. b At first also caught a, but in a let go of the same time B did not let go, then B will not fall;
3. b At first also grabbed a, while a let go of B also let go, then B will fall.

If B falls, it causes the monkeys to fall to the ground as well. Ask each monkey to fall to the ground time. Input Format

The first line of two numbers n, M, indicates that there are n monkeys, and the total time for M-1.

Next N lines, according to the number from 1..N described the information of each monkey, two integers per line, the two number in line I of the monkey's left hand and right hand to catch the number of monkeys, if it is-1, indicating that the monkey hand did not catch other monkeys. And then the M line, in chronological order (0~M-1) gives some information about the monkey letting go, the 1+n+i line represents the i-1 moment of the monkey's release information, which is given in two numbers, which indicates the number of monkeys to let go, the latter indicating which hand they put, and 1 left 2 right. output Format

Total output N row, line I is the first monkey falling moment, if the first monkey to M-1 time after the drop, output-1. Sample Data 1

Input

3 2
-1 3
3-1
1 2
1 2
3 1

Output-1
1
1 Notes

"Data Range"
30% of the data, n≤1000;m≤1000;
100% of the data, 1≤n≤200000;1≤m≤400000.

Problem Solving Report:

Each monkey can be seen as a point in the picture, the relationship between the monkeys with the Benlay said, to determine whether a monkey fell to see if it and 1 in the same connected component. It may be a little difficult to follow the question of thinking, can think in turn, can first find out the M-1 moment after the monkeys, and then upside down, will monkey a only "go" up, so the problem becomes a monkey at the earliest when and 1 of the connected block and together. The maintenance of connectivity can be achieved by using the and searching set.

Time Efficiency:
We use and search the set to achieve connectivity maintenance, the total time efficiency is O (n*α), where α is not more than 5 of the number, can be regarded as a constant.

Space efficiency: 0 (n+m).

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> int n,m,cnt,hand[
200001][2],RET[400001],T[400001][2],GRAPH[400001],TIME[400001][2];
BOOL sign[400001]; struct node{int tvtx,last;}
ARC[2000001];
    inline int readint () {int i=0,f=1;
    Char ch; For (Ch=getchar ();(ch< ' 0 ' | |
    Ch> ' 9 ') && ch!= '-'; Ch=getchar ()); 
        if (ch== '-') {f=-1;
    Ch=getchar ();
    for (; ch>= ' 0 ' && ch<= ' 9 '; Ch=getchar ()) i= (i<<3) + (i<<1) +ch-' 0 ';
return i*f;
	} inline void Add (int a,int b) {arc[++cnt].tvtx=b;
	Arc[cnt].last=graph[a];
graph[a]=cnt;
	} inline void Dfs (int x,int t) {sign[x]=1;
	ret[x]=t;
		for (int i=graph[x];i;i=arc[i].last) {int v=arc[i].tvtx;
	if (!sign[v]) DFS (V,T);
	int main () {register int i,j,a,b;
	N=readint ();
	M=readint ();
	for (I=1;i<=n;++i) Hand[i][0]=readint (), Hand[i][1]=readint ();
	memset (time,-1,sizeof (time));
	for (i=1;i<=n;++i) ret[i]=-2; for (i=0;i<m;++i) {t[i][0]=readint ();
		T[i][1]=readint ();
		Time[t[i][0]][t[i][1]-1]=i;
	T[I][1]=HAND[T[I][0]][T[I][1]-1];
				for (I=1;i<=n;++i) for (j=0;j<=1;++j) if (hand[i][j]!=-1&&time[i][j]==-1) {Add (i,hand[i][j));
			Add (hand[i][j],i);
	DFS (1,-1);
		for (I=m-1;i>=0;--i) {a=t[i][0];
		B=T[I][1];
		Add (a,b);
		Add (B,a);
		if (Sign[a]&&!sign[b]) DFS (b,i);
	if (Sign[b]&&!sign[a]) DFS (a,i);
for (i=1;i<=n;++i) printf ("%d\n", Ret[i]); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.