2 条题解
-
0
博客题解174ms:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int maxn=100233,maxm=1002333; struct zs{ int to,pre; }e[maxm << 1],e1[maxm << 1];int tot,last[maxn],tot1,last1[maxn]; int u[maxm],v[maxm]; int fa[maxn]; int i,j,k,n,m; inline void insert(int a,int b){ if(e[last[a]].to==b)return; e[++tot].to=b,e[tot].pre=last[a],last[a]=tot; } inline void ins(int a,int b){ e1[++tot1].to=b,e1[tot1].pre=last1[a],last1[a]=tot1; } int getfa(int x){ return fa[x]!=x?fa[x]=getfa(fa[x]):x; } void dfs(int x){ fa[x]=x+1; printf("%d\n",x); int next=getfa(1); for(int i=last[x];i;i=e[i].pre){ while(next < e[i].to) dfs(next),next=getfa(next+1); if(next==e[i].to)next=getfa(next+1); } while(next <=n)dfs(next),next=getfa(next+1); } int ra;char rx; inline int read(){ rx=getchar(),ra=0; while(rx < '0'||rx > '9')rx=getchar(); while(rx >= '0'&&rx <= '9')ra*=10,ra+=rx-48,rx=getchar();return ra; } int main(){ n=read(),m=read(); for(i=1;i<=m;i++)u[i]=read(),v[i]=read(),ins(v[i],i),ins(u[i],i); for(i=n;i;fa[i]=i,i--) for(j=last1[i];j;j=e1[j].pre) if(j&1)insert(u[e1[j].to],i); else insert(v[e1[j].to],i); // for(i=1;i<=n;i++)for(j=last[i];j;j=e[j].pre)printf("%d-->%d\n",i,e[j].to);return 233; fa[n+1]=n+1; dfs(1); return 0; }千问AI题解640ms:
#include <cstdio> #include <vector> #include <unordered_set> #include <set> #include <stack> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); vector<unordered_set<int>> deleted(n + 1); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); if (a != b) { deleted[a].insert(b); deleted[b].insert(a); } } set<int> available; for (int i = 2; i <= n; ++i) { available.insert(i); } printf("1\n"); stack<pair<int, int>> stk; stk.push({1, 0}); while (!stk.empty()) { pair<int, int> current = stk.top(); int u = current.first; int last_v = current.second; stk.pop(); set<int>::iterator it = available.upper_bound(last_v); while (it != available.end()) { int v = *it; if (deleted[u].find(v) == deleted[u].end()) { available.erase(v); printf("%d\n", v); stk.push({u, v}); stk.push({v, 0}); break; } ++it; } } return 0; } -
0
博客题解174ms:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int maxn=100233,maxm=1002333; struct zs{ int too,pre; }e[maxm<<1],e1[maxm<<1];int tot,last[maxn],tot1,last1[maxn]; int u[maxm],v[maxm]; int fa[maxn]; int i,j,k,n,m; inline void insert(int a,int b){ if(e[last[a]].too==b)return; e[++tot].too=b,e[tot].pre=last[a],last[a]=tot; } inline void ins(int a,int b){ e1[++tot1].too=b,e1[tot1].pre=last1[a],last1[a]=tot1; } int getfa(int x){ return fa[x]!=x?fa[x]=getfa(fa[x]):x; } void dfs(int x){ fa[x]=x+1; printf("%d\n",x); int next=getfa(1);//printf(" next:%d\n",next); for(int i=last[x];i;i=e[i].pre){ while(next<e[i].too) dfs(next),next=getfa(next+1); if(next==e[i].too)next=getfa(next+1); } while(next<=n)dfs(next),next=getfa(next+1); } int ra;char rx; inline int read(){ rx=getchar(),ra=0; while(rx<'0'||rx>'9')rx=getchar(); while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra; } int main(){ n=read(),m=read(); for(i=1;i<=m;i++)u[i]=read(),v[i]=read(),ins(v[i],i),ins(u[i],i); for(i=n;i;fa[i]=i,i--) for(j=last1[i];j;j=e1[j].pre) if(j&1)insert(u[e1[j].too],i); else insert(v[e1[j].too],i); // for(i=1;i<=n;i++)for(j=last[i];j;j=e[j].pre)printf("%d-->%d\n",i,e[j].too);//return 233; fa[n+1]=n+1; dfs(1); return 0; }千问AI题解640ms:
#include <cstdio> #include <vector> #include <unordered_set> #include <set> #include <stack>
</p>using namespace std;
int main() { // 禁用同步以提升性能(对 cin 有效,但保留不影响) // 注意:对于 scanf/printf,此设置无效 // 保留以兼容旧代码风格 // ios::sync_with_stdio(False); // cin.tie(Noneptr);
int n, m; scanf("%d %d", &n, &m); vector<unordered_set<int>> deleted(n + 1); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); if (a != b) { deleted[a].insert(b); deleted[ b].insert(a); } } set<int> available; for (int i = 2; i <= n; ++i) { available.insert(i); } printf("1\n"); stack<pair<int, int>> stk; stk.push({1, 0}); while (!stk.empty()) { pair<int, int> current = stk.top(); int u = current.first; int last_v = current.second; stk.pop(); set<int>::iterator it = available.upper_bound(last_v); while (it != available.end()) { int v = *it; if (deleted[ u].find(v) == deleted[ u].end()) { available.erase(v); printf("%d\n", v); stk.push({u, v}); stk.push({v, 0}); break; } ++it; } } return 0;}
- 1
信息
- ID
- 2954
- 时间
- 5000ms
- 内存
- 512MiB
- 难度
- 8
- 标签
- 递交数
- 84
- 已通过
- 15
- 上传者