Problem:
Problem B:
Let's JUMPSTYLE
Description
踊りの好きなFは，とあるダンスホールでJUMPSTYLEと呼ばれるハードコアな踊りの練習をすることにした．
フロアはN×Nの格子状にタイルが敷き詰められている．
踊りの練習をサポートするため，各タイルには次のステップに飛び移るべきタイルの座標が書かれている．Fは日々の練習によって強靭な運動神経を持っており，フロア内の任意のタイルへ飛び移ることができる．
Fは，十分に長い時間この踊りを続けていけば，いつか定常状態に入り，同じルートをただループする状態になることに気付いた．Fはこのようなループがこのフロアにいくつ存在するのか疑問に思い，プログラマであるあなたに相談することにした．
Input
入力は複数のテストケースから成る．
各テストケースはフロアの1辺の長さNを含む1行で始まる．(1 ≦ N ≦ 100)
続くN行は，フロアの各タイルに書いてあるジャンプ先の座標を以下のように表している．
は，それぞれ座標(i, j)にあるタイルに書いてあるジャンプ先のx座標およびy座標を表す．座標は0-originであり，すべての座標の値は0以上N未満の整数である．
入力は0のみから成る行で終わる．
Output
各テストケースに対し，異なるループの個数を1行で出力する．
Sample Input
1
0 0
2
1 1 0 1
1 0 0 0
2
1 1 0 1
1 1 1 0
3
0 1 2 2 2 1
0 2 1 2 2 1
0 0 0 1 1 1
4
3 2 2 0 3 2 2 1
1 1 0 3 1 1 3 1
0 3 2 3 3 0 2 3
1 1 1 1 3 2 1 3
0
Output for Sample Input
1
2
1
2
3

Submitted solutions:

code_snippet: #include <iostream>

using namespace std;

int w[101][101],h[101][101];
bool b[101][101];

int dfs(int x,int y)
{
    
    if(x == -1 || y == -1) return 0;
    if(b[x][y]){
        w[x][y] = -1;
        h[x][y] = -1;
        return 1;
    }
    
    b[x][y] = true;
    
    int rec;
    rec = dfs(w[x][y],h[x][y]);
    b[x][y] = false;
    return rec;
}

int main()
{
    int n,sum;
    
    while(cin >> n,n!=0){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cin >> w[j][i] >> h[j][i];
            }
        }
    
        int sum = 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                sum += dfs(i,j);
            }
        }
    
        cout << sum << endl;
    }
        
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3188
final_rank: 13

code_snippet: #include <iostream>
#include <cstring>
#include <stack>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int,int> > vp;
vector<pair<double,int> > member;
const double eps=1e-9;
typedef pair<int,int> p;
bool loop[105][105];
int board[105][205];
vector<p> a;
int main()
{
  int n;
  while(1)
    {
      int ans=0;
      cin >> n;
      if(!n) break;
      memset(loop,false,sizeof(loop));
      for(int i=0;i<n;i++)
	{
	  for(int j=0;j<n;j++)
	    {
	      int x,y;
	      cin >> x >> y;
	      board[i][2*j]=x;
	      board[i][2*j+1]=y;
	    }
	}
      for(int i=0;i<n;i++)
	{
	  for(int j=0;j<n;j++)
	    {
	      set<p> st;
	      st.clear();
	      int nowx=j;
	      int nowy=i;
	      if(loop[i][j]) continue;
	      vector<p> tmp;
	      while(1)
		{
		  //cout << " nowy nowx (" << nowy << ' ' << nowx << " )" << endl;
		  auto it=st.find(p(nowx,nowy));
		  if(it!=st.end())
		    {
		      bool forg=false;
		      for(int k=0;k<tmp.size();k++)
			{
			  if(!loop[tmp[k].second][tmp[k].first]) loop[tmp[k].second][tmp[k].first]=true;
			  else 
			    {
			      forg=true;
			    }
			}
		      if(!forg) ans++;
		      break;
		    }
		  else
		    {
		      // cout << nowy << ' ' << nowx << endl;
		      st.insert(p(nowx,nowy));
		      tmp.push_back(p(nowx,nowy));
		      int newnowx=board[nowy][nowx*2];
		      int newnowy=board[nowy][(nowx*2)+1];
		      nowx=newnowx;
		      nowy=newnowy;
		    }
		}
	    }
	}
      cout << ans << endl;
    }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

bool dfs(int si, int sj, int i, int j, vector<vector<pair<int, int> > > a, vector<vector<bool> >& used, vector<vector<bool> > & used2){

    
    if(i == si && j == sj) return true;

    int nexti = a[i][j].first;
    int nextj = a[i][j].second;

    if(nexti == si && nextj == sj) {
        used[nexti][nextj] = true;
        return true;
    }

    used2[i][j] = true;
    if(used[nexti][nextj]) return false;
    if(used2[nexti][nextj]) return false;

    bool flag = dfs(si, sj, nexti, nextj, a, used, used2);

    if(flag){
        used[i][j] = true;
    }

    return flag;
}

int main(){
    
    while(1){
        int n; cin >> n;
        if(!n) break;
        vector<vector<pair<int, int> > > a(n, vector<pair<int, int> > (n));

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                int x, y; cin >> x >> y;
                a[i][j].first = y;
                a[i][j].second = x;
            }
        }

        vector<vector<bool> > used(n, vector<bool> (n, false));

        int ans = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(!used[i][j]){
                    int nexti = a[i][j].first;
                    int nextj = a[i][j].second;
                    if(used[nexti][nextj]) continue;
                    vector<vector<bool> > used2(n, vector<bool> (n, false));
                    used2[i][j] = true;
                    if(dfs(i, j, nexti, nextj, a, used, used2)){
                        ans++;
                        //cout << i << " " << j << endl;
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 620
memory_kb: 19380
final_rank: 20

code_snippet: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;

class unionfind {
    vector<int> par, rank, size_;
public:
    unionfind(int n) :par(n), rank(n), size_(n, 1) {
        iota(all(par), 0);
    }
    int find(int x) {
        if (par[x] == x)return x;
        return par[x] = find(par[x]);
    }
    void unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y)return;
        if (rank[x] < rank[y])swap(x, y);
        par[y] = x;
        size_[x] += size_[y];
        if (rank[x] == rank[y])rank[x]++;
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        return size_[find(x)];
    }
};

int main(){
    int n;
    while(cin>>n,n){
        int ans=0;
        vector<vector<pii>>graph(n,vector<pii>(n,pii(0,0)));
        rep(i,n)rep(j,n)cin>>graph[i][j].second>>graph[i][j].first;
        vector<vector<bool>>flag(n,vector<bool>(n,true));
        rep(i,n)rep(j,n){
            if(!flag[i][j])continue;
            vector<vector<bool>>temp=flag;
            map<pii,int>mp;
            bool IsHeyro=false;
            pii now=pii(i,j);
            while(1){
                if(mp[now]!=0){ans++; IsHeyro=true;;break;}
                if(flag[now.first][now.second]==false)break;
                mp[now]++;
                now=graph[now.first][now.second];
            }
            if(IsHeyro)for(auto itr=mp.begin(); itr!=mp.end();itr++)flag[itr->first.first][itr->first.second]=false;
        
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 3180
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    while(cin >> n , n){
        int px[n][n] , py[n][n];
        int x , y;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                cin >> px[i][j] >> py[i][j];
        int visited[n][n] = {};
        int ans = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++){
                stack <pair <int , int> > st;
                if(visited[i][j] == 2)continue;
                visited[i][j] = 1;
                st.push(make_pair(i,j));
                int y = py[i][j] , x = px[i][j];
                while(1){
                    if(visited[y][x] != 0){
                        if(visited[y][x] == 1){
                            ans++;
                            visited[y][x] = 2;
                        }
                        while(!st.empty()){
                            if(visited[st.top().first][st.top().second] != 2)
                                visited[st.top().first][st.top().second] = 0;
                            st.pop();
                        }
                        break;
                    }
                    else{
                        visited[y][x] = 1;
                        st.push(make_pair(y,x));
                    }
                    int kari = y;
                    y = py[y][x];
                    x = px[kari][x];
                    kari = y;
                }
            }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 14

code_snippet: #include<iostream>
#include<map>
#include<vector>
using namespace std;
typedef pair<int,int> mypair;
void jump(int x,int y,int acc,int n,vector<vector<bool> >&map,vector<vector<mypair> > &map2){
	if(map[x][y]){
int sx=map2[x][y].first;
int sy=map2[x][y].second;
if(acc==n*n+1)map[x][y]=false;
acc++;
jump(sx,sy,acc,n,map,map2);
}
}

int main(void){
int n;
while(1){
cin>>n;
if(n==0)break;
vector<vector<bool> >map(n,vector<bool>(n,true));
vector<vector<mypair> >map2(n,vector<mypair>(n));
for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
		cin>>map2[j][i].first>>map2[j][i].second;
	}
}int t=0;
for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
jump(i,j,0,n,map,map2);
	}
}
for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
if(map[i][j]==false)t++;
	}
}
cout<<t<<endl;
}
return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 3084
final_rank: 19

code_snippet: #include<bits/stdc++.h>
#define x second
#define y first
using namespace std;
pair<int,int>a[101][101];
int col[101][101];
int n;
void dfs(int y1,int x1){
  col[y1][x1]=1;
  if(!col[a[y1][x1].y][a[y1][x1].x])dfs(a[y1][x1].y,a[y1][x1].x);
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      if(!col[i][j]&&a[i][j].y==y1&&a[i][j].x==x1)
        dfs(i,j);
}
int main(){
  while(cin>>n,n){
    memset(col,0,sizeof(col));
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        cin>>a[i][j].x>>a[i][j].y;
    int sum=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        if(!col[i][j])dfs(i,j),sum++;
    cout<<sum<<endl;
  }
}
accuracy: 1
time_ms: 140
memory_kb: 3204
final_rank: 17

code_snippet: #include<iostream>
#include<queue>
using namespace std;
int x[10000], a, b, n, c[10000], d[10000];
int main() {
	while (true) {
		for (int i = 0; i < 10000; i++) { d[i] = 0; }
		cin >> n; if (n == 0) { break; }
		for (int i = 0; i < n*n; i++) {
			cin >> a >> b;
			x[i] = b * n + a;
		}
		int cnt = 0;
		for (int i = 0; i < n*n; i++) {
			if(d[i]==0)
			for (int j = 0; j < 10000; j++) { c[j] = 0; }
			int cx = i;
			while (true) {
				c[cx] = 1;
				cx = x[cx];
				if (c[cx] == 1) { break; }
			}
			int C = 0;
			for (int j = 0; j < 10000; j++) {
				if (c[j] == 1 && d[j] >= 1) {
					C = d[j];
				}
			}
			if (C == 0) {
				cnt++; C = cnt;
			}
			for (int j = 0; j < 10000; j++) {
				if (c[j] == 1) { d[j] = C; }
			}
		}
		cout << cnt << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 1276
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
bool used[100][100];
int n,p[2][100][100],sx,sy;
bool dfs(int x,int y){
  if(sx==x&&sy==y)return 1;
  if(used[y][x])return 0;
  used[y][x]=1;
  return used[y][x]=dfs(p[0][y][x],p[1][y][x]);
}
int main(){
  while(cin>>n&&n){
    int c=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	cin>>p[0][i][j]>>p[1][i][j],used[i][j]=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)if(!used[i][j])sx=j,sy=i,c+=dfs(p[0][i][j],p[1][i][j]);
    cout<<c<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1248
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

int n;
int x[110][110],y[110][110];
int visited[110][110];

int main(void){
	while(cin >> n,n){
		int ans=0;
		rep(i,n)rep(j,n)  visited[i][j]=0;
		rep(i,n)rep(j,n) cin >> x[i][j] >> y[i][j];
		rep(yy,n)rep(xx,n){
			if(visited[yy][xx]) continue;
			visited[yy][xx]=-1;
			int cx=x[yy][xx],cy=y[yy][xx];
			while(visited[cy][cx]==0){
				visited[cy][cx]=-1;
				int nx=x[cy][cx],ny=y[cy][cx];
				swap(cx,nx),swap(cy,ny);
			}

			int cur=visited[cy][cx];
			if(cur==-1) cur=++ans;
			cx=xx,cy=yy;
			while(visited[cy][cx]==-1){
				visited[cy][cx]=cur;
				int nx=x[cy][cx],ny=y[cy][cx];
				swap(cx,nx),swap(cy,ny);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1296
final_rank: 7

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_NN = MAX_N * MAX_N;

/* typedef */

typedef vector<int> vi;

/* global variables */

int n, nn;
int flds[MAX_NN], ids[MAX_NN];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0) break;
    nn = n * n;

    for (int pos = 0; pos < nn; pos++) {
      int x, y;
      cin >> x >> y;
      flds[pos] = y * n + x;
    }

    memset(ids, 0, sizeof(ids));

    int id = 0;
    vi fs;

    for (int pos0 = 0; pos0 < nn; pos0++) {
      if (ids[pos0] > 0) continue;

      ids[pos0] = ++id;
      fs.clear();
      fs.push_back(pos0);
      
      int pos = flds[pos0];
      while (! ids[pos]) {
	ids[pos] = id;
	fs.push_back(pos);
	pos = flds[pos];
      }

      int id0 = ids[pos];
      if (id0 != id) {
	for (vi::iterator vit = fs.begin(); vit != fs.end(); vit++)
	  ids[*vit] = id0;
	--id;
      }
    }

    cout << id << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1300
final_rank: 8

code_snippet: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int par[20000];
int Rank[20000];

void init(int n){
    rep(i,n){
        par[i]=i;
        Rank[i]=0;
    }
}

int find(int x){
    if(par[x]==x) {
        return x;
    }else{
        return par[x]=find(par[x]);
    }
}

void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y) return;
    
    if(Rank[x]<Rank[y]){
        par[x]=y;
    }else{
        par[y]=x;
        if(Rank[x]==Rank[y])Rank[x]++;
    }
}



int main(){
	int n;
	while(cin>>n){
		if(n==0)break;

		pii data[200][200];
		rep(i,n){
			rep(j,n){
				int a,b;
				cin>>a>>b;
				data[i][j]=make_pair(a,b);
			}
		}

		init(20000);

		rep(i,n){
			rep(j,n){
				unite( n*i+j , n*data[i][j].second+data[i][j].first );
			}
		}
		bool used[20000]={};
		int c=0;
		rep(i,n)rep(j,n)used[ find(n*i+j) ]=true;
		rep(i,20000)if(used[i])c++;
		cout<<c<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 1652
final_rank: 11

code_snippet: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#ifdef Debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif


int par[10000];
int Rank[10000];

void init(int n){
    rep(i,n){
        par[i]=i;
        Rank[i]=0;
    }
}

int find(int x){
    if(par[x]==x) {
        return x;
    }else{
        return par[x]=find(par[x]);
    }
}

void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y) return;
    
    if(Rank[x]<Rank[y]){
        par[x]=y;
    }else{
        par[y]=x;
        if(Rank[x]==Rank[y])Rank[x]++;
    }
}




int main(){
	int n;
	while(cin>>n){
		if(n==0)break;
		pii data[110][110]={};
		rep(i,n)
			rep(j,n)
				cin>>data[i][j].first>>data[i][j].second;

		init(n*n);

		rep(i,n){
			rep(j,n){
				unite(n*i+j,n*data[i][j].second+data[i][j].first);
			}
		}
		bool used[10000]={};
		int sum=0;
		rep(i,n*n){
			int aa=find(i);
			if( used[ aa ]==false ){
				sum++;
				used[aa]=true;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1348
final_rank: 10

code_snippet: #include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<set>
#include<climits>
using namespace std;
typedef vector<string>vs;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<double>vd;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll>pll;
typedef vector<ll>vl;
#define rrep(i,x,n) for(int i=(x);i<(n);++i)
#define rep(i,x) rrep(i,0,(x))
#define fi first
#define se second
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define pb push_back
#define maxs(a,b) (a)=max(a,b)
#define mins(a,b) (a)=min(a,b)

int main(){
    int N;
    while(scanf("%d",&N),N){
        vi V(N*N);
        rep(i,N*N){
            int x,y;
            scanf("%d%d",&x,&y);
            V[i]=y*N+x;
        }
        int cnt=0;
        bool used[10000]={0};
        rep(i,N*N){
            if(used[i])continue;
            int pos=i;
            bool u[10000]={0};
            while(!u[pos]){
                u[pos]=true;
                pos=V[pos];
            }
            if(pos!=i)continue;
            cnt++;
            rep(i,N*N)used[i]|=u[i];
        }
        printf("%d\n",cnt);
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1252
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define ALL(v) (v).begin(), (v).end()

class DisjointSet{
private:
  vector< int > rank, p;
  void link(int x,int y){
    if(rank[x] > rank[y]){
      p[y] = x;
    }else{
      p[x] = y;
      if(rank[x] == rank[y]) rank[y]++;
    }
  }
public:
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    for(int i = 0; i < size; i++){
      makeSet(i);
    }
  }
  void makeSet(int x){
    p[x] = x, rank[x] = 0;
  }
  void Union(int x,int y){
    link(findSet(x),findSet(y));
  }
  int findSet(int x){
    return( x != p[x] ? p[x] = findSet(p[x]) : p[x]);
  }
};

int main()
{
  int N;
  while(scanf("%d", &N), N){
    DisjointSet UnionFind(N * N);
    for(int i = 0; i < N; i++){
      for(int j = 0; j < N; j++){
        int x, y;
        scanf("%d %d", &x, &y);
        UnionFind.Union( i * N + j, y * N + x);
      }
    }
    vector< bool > size(N * N, false);
    for(int i = 0; i < N * N; i++){
      size[UnionFind.findSet(i)] = true;
    }
    cout << count(ALL(size), true) << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1284
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define ALL(v) (v).begin(), (v).end()

class DisjointSet{
private:
  vector<int> rank, p;
  void link(int x,int y){
    if(rank[x] > rank[y]){
      p[y] = x;
    }else{
      p[x] = y;
      if(rank[x] == rank[y]) rank[y]++;
    }
  }
public:
  DisjointSet(int size){
    rank.resize(size,0);
    p.resize(size,0);
    for(int i = 0; i < size; i++){
      makeSet(i);
    }
  }
  void makeSet(int x){
    p[x] = x, rank[x] = 0;
  }
  void Union(int x,int y){
    link(findSet(x),findSet(y));
  }
  int findSet(int x){
    return( x != p[x] ? p[x] = findSet(p[x]) : p[x]);
  }
};

int main()
{
  int N;
  while(cin >> N, N){
    DisjointSet UnionFind(N * N);
    for(int i = 0; i < N; i++){
      for(int j = 0; j < N; j++){
        int x, y;
        cin >> x >> y;
        UnionFind.Union( i * N + j, y * N + x);
      }
    }
    vector< bool > size(N * N, false);
    for(int i = 0; i < N * N; i++){
      size[UnionFind.findSet(i)] = true;
    }
    cout << count(ALL(size), true) << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1276
final_rank: 5

code_snippet: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;


bool visited1[100][100];
bool visited2[100][100];
int dx[100][100];
int dy[100][100];


bool f(int y,int x){
	if(visited1[y][x]){
		return visited2[y][x];
	}
	visited1[y][x]=true;
	visited2[y][x]=true;
	bool res=f(dy[y][x],dx[y][x]);
	visited2[y][x]=false;
	return res;
}

int main(){
	int n;
	while(cin>>n&&n){
		memset(visited1,false,sizeof(visited1));
		for(int y=0;y<n;y++){
			for(int x=0;x<n;x++){
				cin>>dx[y][x]>>dy[y][x];
			}
		}
		int ans=0;
		for(int y=0;y<n;y++){
			for(int x=0;x<n;x++){
				if(!visited1[y][x])ans+=f(y,x);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1264
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
int x[110][110];
int y[110][110];
int v[110][110];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++)
			for(int j=0;j<a;j++)
				scanf("%d%d",&y[i][j],&x[i][j]);
		int ret=0;
		for(int i=0;i<a;i++)
			for(int j=0;j<a;j++)
				v[i][j]=0;
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				int row=i;
				int col=j;
				int s,t;

				while(!v[row][col]){
					v[row][col]=1;
					s=x[row][col];
					t=y[row][col];
					row=s;col=t;
				}
				if(v[row][col]==1)ret++;
				row=i;col=j;
				while(v[row][col]!=2){
					v[row][col]=2;
					s=x[row][col];
					t=y[row][col];
					row=s;col=t;
				}
			}
		}
		printf("%d\n",ret);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1164
final_rank: 1

code_snippet: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
pii board[110][110];
int visit[110][110];

bool check(int px,int py,int level){
	int nx,ny;
	while(visit[py][px] == 0){
		visit[py][px] = level;
		nx = board[py][px].first;
		ny = board[py][px].second;
		px = nx;
		py = ny;
	}
	if(visit[py][px] == level)return true;
	return false;
}

int main(){
	while(cin>>n,n){

		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				cin>>board[i][j].first>>board[i][j].second;
			}
		}
		memset(visit, 0, sizeof(visit));
		int level = 0;
		int ans = 0;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				if(visit[i][j] == 0){
					level++;
					if(check(j, i, level)){
						ans++;
					}
				}
			}
		}
		cout <<ans<<endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1308
final_rank: 9

code_snippet: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb(in,tmp) in.push_back(tmp)
#define all(in) in.begin(),in.end()
#define PI acos(-1)
using namespace std;
int tile[100][200]={0};
int co=100;
void ride(int i,int j,int tile[100][200],int& count){
  int tmp=tile[i][2*j];
  tile[i][2*j]=co;
  if(tile[tile[i][2*j+1]][2*tmp]==co){count++;return;}
  else if(tile[tile[i][2*j+1]][2*tmp]>100&&tile[tile[i][2*j+1]][2*tmp]<co)return;
  else ride(tile[i][2*j+1],tmp,tile,count);

}
int main(){
  int n;
  while(cin>>n,n){
    int count=0;
    rep(i,n){
      rep(j,2*n){
	cin>>tile[i][j];
      }
    }
    rep(i,n){
      rep(j,n){
	co++;
	if(tile[i][2*j]>100)continue;
	ride(i,j,tile,count);
      }
    } 
    cout<<count<<endl;
  }

}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 2