NOIP2005 Bonfire Party

Source: Internet
Author: User

Title Description Description

Allison just into high school, in military training, because Allison hard-working, soon got the instructor's appreciation, became a "small instructors." At the end of the military training that night, Allison was ordered to organize the students to carry out the bonfire party. There are a total of n classmates, numbering from 1 to N. At first, the students sat in a circle in the order of 1,2,......,n, but in fact everyone had two students who wanted to be next to each other. How to adjust the order of the students in order to form a new circle, so that they meet the wishes of the students, become a big problem in the face of Jia Jia.
Allison can give orders to the students, the form of each command is as follows:
(B1,b2,... BM-1,BM)
The value of M here is determined by Allison, each time the value of the command m can be different. The purpose of this command is to move the number is B1,B2, ... bm–1,bm the location of this m-classmate. Request B1 Change to B2 position, B2 change to B3 position, ..., request BM Switch to B1 position.
There is a cost to executing each command. We assume that if a command is to move the position of M person, then the cost of this command is M. We need Allison with the least total cost to achieve the students ' wishes, can you help Allison?

Enter a description Input Description

The input first line is an integer n (3<=n<=50000), indicating that there are altogether n classmates. After the n row each row consists of two different positive integers, separated by a space, the number is 1 of the classmates most want to next to the number of two classmates, the number is 2 of the classmates most want to adjacent to the number of two classmates, ..., the number is n the classmate most want to next two classmates number.

Output description Output Description

This line contains only an integer, which is the minimum total cost. If no matter how the adjustment can not meet the wishes of each classmate, then output-1.

Sample input Sample Input

4

3 4

4 3

1 2

1 2

Sample output Sample Output

2

Data range and Tips Data Size & Hint


"Data Size"



For 30% of data,n<=1000;

For all the data, n<=50000.

#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<stack>using namespacestd;Const intMAXN =50005;intn,gay[maxn][2],sb[maxn],sum[maxn],dis[maxn],ans =0;;BOOLVIS[MAXN];voidinput () {Ios::sync_with_stdio (false); CIN>>N;  for(inti =1; I <= n;i++) {cin>>gay[i][0]>>gay[i][1]; }}voidsolve () {intlast,now,tmp;  for(inti =1; I <= n;i++){        if(i = =1) { now=1; Sb[i]=Now ; Last=Now ; now= gay[last][0]; }Else{            if(now = =Last ) {cout<<-1; Exit (0); } Sb[i]=Now ; TMP=Now ; now= (gay[now][0] = = last? gay[now][1]: gay[now][0]); Last=tmp; }    }    if(Now! =1) {cout<<-1; Exit (0); }    intMAXV =0; //for (int i = 1;i <= n;i++) cout<<sb[i]<< "";     for(inti =1; I <= n;i++){        if(Sb[i] = =i) {dis[0]++; MAXV= Max (dis[0],MAXV); }        if(I <Sb[i]) {Dis[i+n-sb[i]]++; MAXV= Max (dis[i+n-SB[I]],MAXV); }        if(I >Sb[i]) {Dis[i-sb[i]]++; MAXV= Max (dis[i-SB[I]],MAXV); }} ans= NMAXV; return;}voidsolve2 () {memset (SB),0,sizeof(SB)); memset (DIS,0,sizeof(DIS)); intlast,now,tmp;  for(inti =1; I <= n;i++){        if(i = =1) { now=1; Sb[i]=Now ; Last=Now ; now= gay[last][1]; }Else{            if(now = =Last ) {cout<<-1; return; } Sb[i]=Now ; TMP=Now ; now= (gay[now][0] = = last? gay[now][1]: gay[now][0]); Last=tmp; }    }    if(Now! =1) {cout<<-1; return; }    intMAXV =0; //for (int i = 1;i <= n;i++) cout<<sb[i]<< "";     for(inti =1; I <= n;i++){        if(Sb[i] = =i) {dis[0]++; MAXV= Max (dis[0],MAXV); }        if(I <Sb[i]) {Dis[i+n-sb[i]]++; MAXV= Max (dis[i+n-SB[I]],MAXV); }        if(I >Sb[i]) {Dis[i-sb[i]]++; MAXV= Max (dis[i-SB[I]],MAXV); }} ans= MIN (nMaxv,ans); cout<<ans; return;}intMain () {input ();    Solve ();    Solve2 (); return 0;}

NOIP2005 Bonfire Party

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.