2 solutions
-
1
-
#include<bits/stdc++.h> using namespace std; int n,m,a[1000010],b[1000010],ans; bool c[1000010]; vector<int> v[10010]; int cnt; void dfs(int x){ ans=max(ans,x-1); cnt++; if(cnt>2e8){ printf("%d",ans); exit(0); } for(int i=0;i<int(v[x].size());i++){ int u=v[x][i]; if(c[u]==1) continue; c[u]=1; dfs(x+1); c[u]=0; } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i],&b[i]); m=max(m,max(a[i],b[i])); v[a[i]].push_back(i); if(a[i]!=b[i]) v[b[i]].push_back(i); } dfs(1); printf("%d",ans); }
-
-
-1
数据水的一批。爆搜过了。
首先写一个正常的 dfs,TLE 50 pts。
然后让 dfs 函数运行一定次数后直接输出当前最大答案,并结束程序,然后就过了。
代码:
#include<bits/stdc++.h> using namespace std; int n,m,a[1000010],b[1000010],ans; bool c[1000010]; vector<int> v[10010]; int cnt; void dfs(int x){ ans=max(ans,x-1); cnt++; if(cnt>2e8){ printf("%d",ans); exit(0); } for(int i=0;i<int(v[x].size());i++){ int u=v[x][i]; if(c[u]==1) continue; c[u]=1; dfs(x+1); c[u]=0; } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i],&b[i]); m=max(m,max(a[i],b[i])); v[a[i]].push_back(i); if(a[i]!=b[i]) v[b[i]].push_back(i); } dfs(1); printf("%d",ans); }
- 1
Information
- ID
- 631
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 5
- Tags
- # Submissions
- 6
- Accepted
- 4
- Uploaded By