Problem:
両端にリングのついた紐を考える.リングは正整数が付いていて,紐を区別する.紐の両端のリングには異なる数
a
,
b
が付けられる.これを,[
a
,
b
] と記述する.複数の紐があり,一方の紐と他方の紐のリングに付いている数が同じ場合,そのリングのところで,これらの紐をつなげることができて,つなげてできたものを鎖と呼ぶことにする.例えば,[1, 3] と [3, 4] という紐からは [1, 3, 4] という鎖ができる.紐と鎖や鎖同志も同じ整数が付いているリングのところでつなげることができるものとする.
例えば,鎖 [1, 3, 4] と紐 [5, 1] からは [5, 1, 3, 4] ができ,鎖 [1, 3, 4] と鎖 [2, 3, 5] からは, 中央でクロスしたような形になる.鎖 [1, 3, 4] と鎖 [4, 6, 1] からは, 輪の形ができる.
このように様々な形ができるが, そのうちの一部で,同じ数の付いたリングは一度だけたどるつながった複数の紐を鎖と呼ぶことにする.例えば,鎖 [1, 3, 4] と鎖[2, 3, 5] からできる,中央でクロスしたような形には, [1, 3, 5],[2, 3, 4] という鎖も含まれ,鎖 [1, 3, 4] と鎖 [4, 6, 1] からできる輪には, [1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]などの鎖が含まれる.
これらの鎖に対して,含まれる数の個数を長さと定義する. 与えられた複数の紐に対して,つなげられるものはすべてつなげると1つ以上の
鎖を含む形ができる.そのうちの最大の鎖の長さを求めるプログラムを作成せよ.
入力データ の最初の行には紐の個数である正整数 1 ≤
n
≤ 100 が書いてあり,つづく
n
行のそれぞれには, 空白で区切られた 2 つの整数
a
,
b
が書いてあり 1 ≤
a
<
b
≤  100 となっている.各行の 2 つの整数は 1 つの紐の両端の整数を表わす.
入力例１
入力例２
入力例３
7
6
7
1 3
1 2
1 3
3 4
2 3
2 4
1 4
3 4
3 5
2 7
4 5
4 6
5 7
1 5
6 7
6 7
2 6
2 6
1 7
4 7
出力例１
出力例２
出力例３
5
6
4
入力
入力は複数のデータセットからなる．n が 0 のとき入力が終了する．データセットの数は 10 を超えない．
出力
データセットごとに、最大の鎖の長さを１行に出力する．
入力例
7
1 3
3 4
1 4
2 7
5 7
6 7
1 7
6
1 2
2 3
3 4
4 5
1 5
2 6
7
1 3
2 4
3 5
4 6
6 7
2 6
4 7
0
出力例
5
6
4
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static constexpr int MAXV = 100;
vector<int> adj[MAXV+1];
bool used[MAXV+1];
int bestLen;

// 深さ優先探索で「単純パス」の最大辺数を更新
void dfs(int u, int depth) {
    bestLen = max(bestLen, depth);
    for (int v : adj[u]) {
        if (!used[v]) {
            used[v] = true;
            dfs(v, depth + 1);
            used[v] = false;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while (true) {
        int n;
        if (!(cin >> n) || n == 0) {
            break;  // 入力終了
        }

        // グラフの初期化
        for (int i = 1; i <= MAXV; ++i) {
            adj[i].clear();
        }

        // 辺の入力
        for (int i = 0; i < n; ++i) {
            int a, b;
            cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }

        bestLen = 0;
        // 各頂点をスタートに DFS
        for (int start = 1; start <= MAXV; ++start) {
            if (adj[start].empty()) continue;
            fill(begin(used), end(used), false);
            used[start] = true;
            dfs(start, 0);
        }

        // bestLen は最大「辺数」なので、頂点数に直すには +1
        cout << (bestLen + 1) << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 1900
memory_kb: 3456
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MM = 100;
typedef __int128 ll;
int N, dep = 0, ans = 2;
vector<int> ajl[MM+1];
map<ll, int> dp[MM];
bool vis[MM+1];

int dfs(int root, ll soFar, int dep){
    if (dp[root].count(soFar)) return dp[root][soFar];
    dp[root][soFar] = dep;
    for (int i : ajl[root]) if ((soFar&((ll)1<<i)) == 0) dp[root][soFar] = max(dp[root][soFar], dfs(i, soFar | ((ll)1<<i), dep+1));
    return dp[root][soFar];
}

void solve1(){
    for (int i = 1, a, b; i <= N; i++){
        cin >> a >> b;
        a--; b--;
        ajl[a].push_back(b);
        ajl[b].push_back(a);
    }
    for (int i = 0; i < 100; i++) ans = max(ans, dfs(i, (ll)1<<i, 1));

    cout << ans << "\n";
}

void dfs2(int root, int d){
    vis[root] = 1; dep = max(dep, d);
    for (int i : ajl[root]){
        if (vis[i]==1) continue;
        dfs2(i, d+1);
    }
    vis[root] = 0;
}

void solve2(){
    for (int i = 1; i <= MM; i++) ajl[0].push_back(i);
    for (int i = 1, a, b; i <= N; i++){
        cin >> a >> b;
        ajl[a].push_back(b);
        ajl[b].push_back(a);
    }
    dfs2(0, 0);
    cout << dep << "\n";
}

int main()
{
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> N;
    while (N != 0){
        for (int i = 0; i <= MM; ++i) ajl[i].resize(0);
        dep = 0;
        //if (N <= 50)
        solve2();
        //else solve1();
        cin >> N;
    }
}
accuracy: 1
time_ms: 2330
memory_kb: 3364
final_rank: 14

code_snippet: #include <cstdio>
#include <vector>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
vvi adj;
int ans, cnt;
bool visited[100];
void dfs(int u) {
  if (ans < cnt)
    ans = cnt;
  cnt++;
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    if (!visited[v]) {
      visited[v] = true;
      dfs(v);
      visited[v] = false;
    }
  }
  cnt--;
}
int main() {
  for (int m; scanf("%d", &m), m;) {
    bool adjm[100][100] = {}, flg[100] = {};
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      adjm[a][b] = adjm[b][a] = flg[a] = flg[b] = true;
    }
    int f[100], n = 0;
    rep(i, 100) if (flg[i]) f[i] = n++;
    adj = vvi(n);
    rep(i, 100) rep(j, 100) if (adjm[i][j]) adj[f[i]].pb(f[j]);
    ans = 0, cnt = 1;
    rep(u, n - 1) {
      visited[u] = true;
      dfs(u);
      visited[u] = false;
    }
    printf("%d\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 3080
memory_kb: 2600
final_rank: 2

code_snippet: #include <cstdio>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
bool visited[100];
int adj[100][100], deg[100], ans, cnt;
void dfs(int u) {
  if (ans < cnt)
    ans = cnt;
  cnt++;
  rep(i, deg[u]) {
    int v = adj[u][i];
    if (!visited[v]) {
      visited[v] = true;
      dfs(v);
      visited[v] = false;
    }
  }
  cnt--;
}
int main() {
  for (int m; scanf("%d", &m), m;) {
    bool adjm[100][100] = {}, flg[100] = {};
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      adjm[a][b] = adjm[b][a] = flg[a] = flg[b] = true;
    }
    int f[100], n = 0;
    rep(i, 100) if (flg[i]) {
      f[i] = n++;
      deg[f[i]] = 0;
    }
    rep(i, 100) rep(j, 100) if (adjm[i][j]) adj[f[i]][deg[f[i]]++] = f[j];
    ans = 0, cnt = 1;
    rep(u, n - 1) {
      visited[u] = true;
      dfs(u);
      visited[u] = false;
    }
    printf("%d\n", ans);
  }
  return 0;
}
accuracy: 1
time_ms: 2740
memory_kb: 2664
final_rank: 3

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
int edge[100][100];
int size[100];
bool used[100];
int rec(int idx) {
  int ret = 1;
  for (int i = 0; i < size[idx]; i++) {
    if (!used[edge[idx][i]]) {
      used[edge[idx][i]] = true;
      ret = max(ret, rec(edge[idx][i]) + 1);
      used[edge[idx][i]] = false;
    }
  }
  return ret;
}
int main() {
  int N;
  while (cin >> N, N) {
    fill_n(size, 100, 0);
    fill_n(used, 100, false);
    for (int i = 0; i < N; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      edge[a][size[a]++] = b;
      edge[b][size[b]++] = a;
    }
    int ret = 0;
    for (int i = 0; i < 100; i++) {
      used[i] = true;
      ret = max(ret, rec(i));
      used[i] = false;
    }
    cout << ret << endl;
  }
}
accuracy: 1
time_ms: 2830
memory_kb: 3116
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<int> G[101];
bool V[101] = {};
int N;
int Ans = 0;
void DFS(int n, int len) {
  Ans = max(Ans, len);
  bool Vis = false;
  int S = G[n].size();
  for (int i = 0; i < S; i++) {
    if (!V[G[n][i]]) {
      Vis = true;
      V[G[n][i]] = true;
      DFS(G[n][i], len + 1);
      V[G[n][i]] = false;
    }
  }
  return;
}
int main() {
  do {
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      V[i] = false;
    }
    Ans = 0;
    scanf("%d", &N);
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      int A, B;
      scanf("%d%d", &A, &B);
      G[A].push_back(B);
      G[B].push_back(A);
    }
    for (int i = 1; i <= 100; i++) {
      V[i] = true;
      DFS(i, 1);
      V[i] = false;
    }
    printf("%d\n", Ans);
  } while (true);
}
accuracy: 1
time_ms: 3120
memory_kb: 3264
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<int> G[101];
bool V[101] = {};
bool Vis[101] = {};
int DP[101] = {};
int N;
int Ans = 0;
void DFS(int n, int len) {
  Ans = max(Ans, len);
  DP[n] = max(DP[n], len);
  int S = G[n].size();
  for (int i = 0; i < S; i++) {
    if (!V[G[n][i]]) {
      Vis[G[n][i]] = true;
      V[G[n][i]] = true;
      DFS(G[n][i], len + 1);
      V[G[n][i]] = false;
    }
  }
  return;
}
int main() {
  do {
    for (int i = 0; i < 101; i++) {
      G[i].clear();
      V[i] = false;
      Vis[i] = false;
      DP[i] = 0;
    }
    Ans = 0;
    scanf("%d", &N);
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      int A, B;
      scanf("%d%d", &A, &B);
      G[A].push_back(B);
      G[B].push_back(A);
    }
    for (int i = 1; i <= 100; i++) {
      if (!Vis[i]) {
        Vis[i] = true;
        V[i] = true;
        DFS(i, 1);
        V[i] = false;
      }
    }
    for (int i = 1; i <= 100; i++) {
      if (DP[i] == Ans) {
        V[i] = true;
        DFS(i, 1);
        V[i] = false;
        break;
      }
    }
    printf("%d\n", Ans);
  } while (true);
}
accuracy: 1
time_ms: 130
memory_kb: 3232
final_rank: 4

code_snippet: #include <cstdio>
#include <stack>
#include <vector>
using namespace std;
vector<int> vec[101];
bool ch[101] = {};
bool visited[101] = {};
int dp[101] = {};
int maxx = -1;
int maxNODE = -1;
void DFS(int i, int val) {
  if (val > maxx)
    maxx = val;
  dp[i] = max(dp[i], val);
  int len = vec[i].size();
  for (int k = 0; k < len; k++) {
    if (!ch[vec[i][k]]) {
      visited[vec[i][k]] = true;
      ch[vec[i][k]] = true;
      DFS(vec[i][k], val + 1);
      ch[vec[i][k]] = false;
    }
  }
}
int main() {
  maxx = -1;
  maxNODE = -1;
  int n, a, b;
  scanf("%d", &n);
  if (n == 0)
    return 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
    if (a > b)
      if (maxNODE < a)
        maxNODE = a;
    if (a < b)
      if (maxNODE < b)
        maxNODE = b;
  }
  for (int i = 1; i <= maxNODE; i++) {
    visited[i] = true;
    ch[i] = true;
    DFS(i, 1);
    ch[i] = false;
  }
  for (int i = 1; i <= maxNODE; i++) {
    if (maxx == dp[i]) {
      ch[i] = true;
      DFS(i, 1);
      ch[i] = false;
      break;
    }
  }
  printf("%d\n", maxx);
  for (int i = 1; i <= maxNODE; i++) {
    visited[i] = false;
    vec[i].clear();
  }
  main();
}
accuracy: 1
time_ms: 3320
memory_kb: 2784
final_rank: 6

code_snippet: #include <cstdio>
#include <stack>
#include <vector>
using namespace std;
vector<int> vec[101];
bool ch[101] = {};
bool visited[101] = {};
int dp[101] = {};
int maxx = -1;
int maxNODE = -1;
void DFS(int i, int val) {
  if (val > maxx)
    maxx = val;
  dp[i] = max(dp[i], val);
  int len = vec[i].size();
  for (int k = 0; k < len; k++) {
    if (!ch[vec[i][k]]) {
      visited[vec[i][k]] = true;
      ch[vec[i][k]] = true;
      DFS(vec[i][k], val + 1);
      ch[vec[i][k]] = false;
    }
  }
}
int main() {
  maxx = -1;
  maxNODE = -1;
  int n, a, b;
  scanf("%d", &n);
  if (n == 0)
    return 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
    if (a > b)
      if (maxNODE < a)
        maxNODE = a;
    if (a < b)
      if (maxNODE < b)
        maxNODE = b;
  }
  for (int i = 1; i <= maxNODE; i++) {
    if (!visited[i]) {
      visited[i] = true;
      ch[i] = true;
      DFS(i, 1);
      ch[i] = false;
    }
  }
  for (int i = 1; i <= maxNODE; i++) {
    if (maxx == dp[i]) {
      ch[i] = true;
      DFS(i, 1);
      ch[i] = false;
      break;
    }
  }
  printf("%d\n", maxx);
  for (int i = 1; i <= maxNODE; i++) {
    visited[i] = false;
    dp[i] = 0;
    vec[i].clear();
  }
  main();
}
accuracy: 1
time_ms: 130
memory_kb: 2576
final_rank: 1

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin >> n, n){
        vector<vector<int>> g(100);
        for(int i = 0, u, v; i < n; i++){
            cin >> u >> v;
            g[--u].push_back(--v);
            g[v].push_back(u);
        }
        int ans = 0;
        vector<bool> used(100);
        function<void(int,int)> dfs = [&](int v, int d){
            used[v] = true;
            ans = max(ans, d);
            for(auto &&u:g[v]){
                if(used[u])continue;
                dfs(u, d + 1);
            }
            used[v] = false;
        };
        for(int i = 0; i < 100; i++)dfs(i, 1);
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 3470
memory_kb: 3224
final_rank: 17

code_snippet: #include <cstdio>
#include <vector>
using namespace std;

int max_;
vector<int> g[101];
int visited[101];

void dfs(int v, int d) {
  visited[v] = 1;
  max_ = max(max_, d);
  for (int i = 0; i < g[v].size(); ++i) {
    if (visited[g[v][i]])
      continue;
    dfs(g[v][i], d + 1);
  }
  visited[v] = 0;
}

int main() {
  for (;;) {
    int N;
    scanf("%d", &N);
    if (N == 0)
      break;
    for (int i = 1; i <= 100; ++i)
      g[i].clear();
    while (N-- > 0) {
      int a, b;
      scanf("%d %d", &a, &b);
      g[a].push_back(b);
      g[b].push_back(a);
    }
    max_ = 0;
    for (int v = 1; v <= 100; ++v)
      dfs(v, 1);
    printf("%d\n", max_);
  }
  return 0;
}
accuracy: 1
time_ms: 2970
memory_kb: 2796
final_rank: 5

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

int max_depth = 0;
const int max_num_vertexes = 100;


void dfs(const vector<vector<int>>& adjacency_list, int v, int depth, bitset<max_num_vertexes>& is_visited) {
	for (const int& u : adjacency_list[v]) {
		if (!is_visited[u]) {
			is_visited[u] = 1;
			dfs(adjacency_list, u, depth + 1, is_visited);
			is_visited[u] = 0;
		}
	}
	max_depth = max(max_depth, depth);
	return;
}

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int num_vertexes = 0;
		vector<pair<int, int>> edges;
		vector<vector<int>> adjacency_list;

		cin >> n;
		if (n == 0) { break; }
		
		for (int i = 0; i < n; i++) {
			int u, v;

			cin >> u >> v;
			u--, v--;
			edges.emplace_back(make_pair(u, v));
			num_vertexes = max(num_vertexes, u);
			num_vertexes = max(num_vertexes, v);
		}
		num_vertexes++;
		
		adjacency_list = vector<vector<int>>(num_vertexes);
		for (const pair<int, int>& edge : edges) {
			adjacency_list[edge.first].emplace_back(edge.second);
			adjacency_list[edge.second].emplace_back(edge.first);
		}
		
		max_depth = 0;
		for (int v = 0; v < num_vertexes; v++) {
			if (adjacency_list[v].empty()) { continue; }
			bitset<max_num_vertexes> is_visited(0);

			is_visited[v] = 1;
			dfs(adjacency_list, v, 1, is_visited);
		}
		cout << max_depth << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 2970
memory_kb: 3384
final_rank: 18

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

int result = 0;
const int max_num_vertexes = 100;


void dfs(const vector<vector<int>>& adjacency_list, int v, int depth, bitset<max_num_vertexes>& is_visited) {
	bool is_updated = false;

	for (const int& u : adjacency_list[v]) {
		if (!is_visited[u]) {
			is_visited[u] = 1;
			dfs(adjacency_list, u, depth + 1, is_visited);
			is_visited[u] = 0;
		}
	}
	if (!is_updated) {
		result = max(result, depth);
	}
	return;
}

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int num_vertexes = 0;
		vector<pair<int, int>> edges;
		vector<vector<int>> adjacency_list;
		int max_depth = 1;

		cin >> n;
		if (n == 0) { break; }
		
		for (int i = 0; i < n; i++) {
			int u, v;

			cin >> u >> v;
			u--, v--;
			edges.emplace_back(make_pair(u, v));
			num_vertexes = max(num_vertexes, u);
			num_vertexes = max(num_vertexes, v);
		}
		num_vertexes++;
		
		adjacency_list = vector<vector<int>>(num_vertexes);
		for (const pair<int, int>& edge : edges) {
			adjacency_list[edge.first].emplace_back(edge.second);
			adjacency_list[edge.second].emplace_back(edge.first);
		}
		
		result = 0;
		for (int v = 0; v < num_vertexes; v++) {
			if (adjacency_list[v].empty()) { continue; }
			bitset<max_num_vertexes> is_visited(0);
			int depth = -1;

			is_visited[v] = 1;
			dfs(adjacency_list, v, 1, is_visited);
		}
		cout << result << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 2980
memory_kb: 3148
final_rank: 11

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

int result = 0;


void dfs(const vector<vector<int>>& adjacency_list, int v, int depth, bitset<100>& is_visited) {
	bool is_updated = false;

	for (const int& u : adjacency_list[v]) {
		if (!is_visited[u]) {
			is_visited[u] = 1;
			dfs(adjacency_list, u, depth + 1, is_visited);
			is_visited[u] = 0;
		}
	}
	if (!is_updated) {
		result = max(result, depth);
	}
	return;
}

int main() {
#if READ_FROM_FILE
	ifstream ifs("test1.txt");
	cin.rdbuf(ifs.rdbuf());
#endif
	while (true) {
		int n = -1;
		int num_vertexes = 0;
		vector<pair<int, int>> edges;
		vector<vector<int>> adjacency_list;
		int max_depth = 1;

		cin >> n;
		if (n == 0) { break; }
		
		for (int i = 0; i < n; i++) {
			int u, v;

			cin >> u >> v;
			u--, v--;
			edges.emplace_back(make_pair(u, v));
			num_vertexes = max(num_vertexes, u);
			num_vertexes = max(num_vertexes, v);
		}
		num_vertexes++;
		
		adjacency_list = vector<vector<int>>(num_vertexes);
		for (const pair<int, int>& edge : edges) {
			adjacency_list[edge.first].emplace_back(edge.second);
			adjacency_list[edge.second].emplace_back(edge.first);
		}
		
		result = 0;
		for (int v = 0; v < num_vertexes; v++) {
			if (adjacency_list[v].empty()) { continue; }
			bitset<100> is_visited(0);
			int depth = -1;

			is_visited[v] = 1;
			dfs(adjacency_list, v, 1, is_visited);
		}
		cout << result << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 2980
memory_kb: 3152
final_rank: 12

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

struct UnionFind{
    vector<int> par,num;
    UnionFind(int n):par(n),num(n,1){
        iota(par.begin(),par.end(),0);  //include<numeric>
    }
    int find(int v){
        return (par[v]==v)?v:(par[v]=find(par[v]));
    }
    void unite(int u,int v){
        u=find(u),v=find(v);
        if(u==v)return;
        if(num[u]<num[v])swap(u,v);
        num[u]+=num[v];
        par[v]=u;
    }
    bool same(int u,int v){
        return find(u) == find(v);
    }
    int size(int v){
        return num[find(v)];
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m;
    while(cin >> m,m){
        int n = 100;
        vector<vector<int>> g(n);
        UnionFind uf(n);
        for(int i=0;i<m;i++){
            int a,b; cin >> a >> b;
            a--; b--;
            g[a].push_back(b);
            g[b].push_back(a);
            uf.unite(a, b);
        }
        int res = 0;
        vector<int> dp(n);
        vector<bool> used(n);
        auto dfs=[&](auto dfs,int s,int cur)->void{
            if(uf.size(s) <= res)return;
            dp[s] = max(dp[s],cur);
            res = max(res, cur);
            for(int t:g[s]){
                if(used[t])continue;
                used[t] = true;
                dfs(dfs,t,cur+1);
                used[t] = false;
            }
        };
        for(int i=0;i<n;i++){
            if(uf.find(i) == i){
                used[i] = true;
                dfs(dfs,i,1);
                used[i] = false;
            }
        }
        int u = 0;
        for(int i=0;i<n;i++){
            if(dp[u] < dp[i]) u = i;
        }
        used[u] = true;
        dfs(dfs,u,1);
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 110
memory_kb: 3356
final_rank: 7

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m;
    while(cin >> m,m){
        int n = 100;
        vector<vector<int>> g(n);
        for(int i=0;i<m;i++){
            int a,b; cin >> a >> b;
            a--; b--;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        int res = 0;
        vector<bool> used(n);
        auto dfs=[&](auto dfs,int s,int cur)->void{
            res = max(res, cur);
            for(int t:g[s]){
                if(used[t])continue;
                used[t] = true;
                dfs(dfs,t,cur+1);
                used[t] = false;
            }
        };
        for(int i=0;i<n;i++){
            fill(used.begin(), used.end(), false);
            used[i] = true;
            dfs(dfs,i,1);
        }
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 3360
memory_kb: 3472
final_rank: 19

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

int dfs(int p, vector<bool>& used, vector<vector<int>>& g){
    if(used[p]) return 0;
    used[p] = true;
    int res = 0;
    for(auto v: g[p]){
        res = max(res, dfs(v, used, g));
    }
    used[p] = false;
    return res+1;
}

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

        vector<vector<int>> g(101);
        for(int i=0; i<n; i++){
            int a,b;
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        for(int i=1; i<=100; i++){
            g[0].push_back(i);
        }
        vector<bool> used(101, false);
        int res = dfs(0, used, g);
        cout << res-1 << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 5240
memory_kb: 3352
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n = 100, m;
    while(cin >> m, m){
        vector<vector<int>> G(n+1);
        for (int i = 0; i < m; ++i) {
            int a, b;
            scanf("%d %d", &a, &b);
            a--; b--;
            G[a].emplace_back(b);
            G[b].emplace_back(a);
        }
        for (int i = 0; i < n; ++i) {
            G[n].emplace_back(i);
        }
        int ans = 0;
        vector<int> visited(n);
        auto dfs = [&](int x, int l, auto &&f) -> void {
            ans = max(ans, l);
            for (auto &&i : G[x]) {
                if(!visited[i]){
                    visited[i] = 1;
                    f(i, l+1, f);
                    visited[i] = 0;
                }
            }
        };
        dfs(n, 0, dfs);
        cout << ans << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 2570
memory_kb: 3252
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
using namespace std;
bool vis[101] = {};int ans;
vector<int> v[101];
void dfs(int x, int l){
    vis[x] = 1;ans = max(ans, l);
    for(int j : v[x]){
        if(!vis[j]) dfs(j, l+1);
    }vis[x] = 0;// 終わったら戻す
}
int main(){
    int n;
    while(cin >> n, n){
        for(int i = 1; i <= 100; i++)   v[i].clear();
        for(int i = 1; i <= n; i++){
            int a, b;cin >> a >> b;
            v[a].push_back(b);v[b].push_back(a);
        }ans = 0;
        for(int i = 1; i <= 100; i++)dfs(i, 1);
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 2580
memory_kb: 3104
final_rank: 8

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

#define MAX 101
int N, ans;
bool seen[MAX];
vector<int> M[MAX];

void dfs(int u, int d){
    seen[u] = true;
    ans = max(ans, d);
    vector<int>::iterator it;
    for(it = M[u].begin(); it != M[u].end(); it++){
        if(seen[*it]) continue;
        else dfs(*it, d + 1);
    }
    seen[u] = false;
    return;
}



int main(){
    while(cin >> N){
        if(N == 0) return 0;
        ans = 0;
        for(int i = 1; i <= MAX; i++) M[i].clear();
        for(int i = 1; i <= N; i++){
            int u, v;
            cin >> u >> v;
            M[u].push_back(v);
            M[v].push_back(u);
        }
        for(int i = 1; i <= MAX; i++){
            for(int j = 1; j <= MAX; j++) seen[j] = false;
            dfs(i, 1);
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 2580
memory_kb: 3108
final_rank: 9