Codeforces 583A Asphalting Roads

A. Asphalting Roads
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.

Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.

Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.

According to the schedule of road works tell in which days at least one road will be asphalted.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city.

Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.

Output

In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.

Sample test(s)
input
2
1 1
1 2
2 1
2 2
output
1 4 
input
1
1 1
output
1 
Note

In the sample the brigade acts like that:

  1. On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road;
  2. On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything;
  3. On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything;
  4. On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.


智商題,想明白了很簡單,先想i==a[i]的情況,如果有的話,那麼一定能成功,因爲a[i]和其他任意配,對子相應的序號對一定也是a[i]和其他的配對,故直接做。再想除去以上情況,a[a[i]]=i 即1(2) 2(1)這種。我們把序號和對應數字連有向線,必是一個又一個的環,因爲二元環本身可以滿足題目條件, 和i==a[i]的情況一樣,我們能不能把這些環跟選定的二元環都連在一起呢,經過測試,我發現x元環和二元環連接的必要條數是2和x的最小公倍數,由於題目要求的是一棵樹,所以凡是非2整數倍的元環均不能與2元環相連,所以在有二元環的情況下,看其他元環的節點數,若全部是2的整數倍,那麼題目就有解。


有人會玩其他元環可不可能也能當頭元環,答案是否定的,因爲3元環以上,元環自身均不能滿足題目條件,談何去連接別人呢。


代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<map>
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mod 1000000007
using namespace std;
int n,a[100020],x,y,m;
bool f[100020];
int se[100020][2];
vector<int> v;
void fun1(int x){
  int i,j;
  
  m=0;
  rep(i,n){
    if(f[i]==1) continue;
	m++;
	se[m][0]=x; se[m][1]=a[i];	
  }	
}
void dfs(int ii){
  int i,j;
  
  if(f[ii]) return;
  f[ii]=1; v.push_back(a[ii]);
  dfs(a[ii]);	
}
void fun2(int x,int y){
  int i,j;
  
  m=1;
  se[m][0]=x; se[m][1]=y;
  rep(i,n){
  	if(f[i]==1) continue;
  	v.clear();
  	dfs(i);
  	if(v.size()%2==1){
	  m=-1;
	  return;  	
    }
    for(j=0;j<v.size();j++){
      m++;
	  se[m][0]=j%2?x:y; se[m][1]=v[j];	
    }
  }
}
int main(){
	int i,j;
	
    while(scanf("%d",&n)!=EOF){
   	  MM(f,0); m=-1;
      rep(i,n) scanf("%d",&a[i]);
	  rep(i,n){
  	    if(i==a[i]){
    	  f[i]=1;
		  fun1(a[i]);  	
		  break;
	    }	
  	  } 	
  	  if(m==-1){
  	  	rep(i,n)
	    if(a[a[i]]==i){
    	  f[i]=1;
		  f[a[i]]=1;
		  fun2(i,a[i]);	
		  break;
    	}
  	  }
  	  if(m==-1) printf("NO\n");
  	  else{
  	  	printf("YES\n");
  	    rep(i,m) printf("%d %d\n",se[i][0],se[i][1]);	
  	  }
    }
	
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章