QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857679 | #9911. 路南柯 | Crysfly | 100 ✓ | 17ms | 38940kb | C++11 | 2.6kb | 2025-01-15 23:02:14 | 2025-01-15 23:02:33 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define ull unsigned long long
//#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 500005
#define inf 0x3f3f3f3f
int n;
vi e[maxn],g1[maxn],g2[maxn];
vector<vi>out;
bool vis[maxn];
vi que;
bitset<105>bs[105],tmp;
bool add(vi p){
tmp.set();
int hav=0;
For(i,0,n-1){
tmp.reset(p[i]);
bs[p[i]]&=tmp;
hav|=(bs[p[i]].count()>1);
}
return !hav;
}
void dfs(int u,int pa){
que.pb(u);
for(int v:g1[u]) if(v!=pa) dfs(v,u);
for(int v:g2[u]) if(v!=pa) dfs(v,u);
reverse(ALL(g1[u]));
}
void my1(){
For(i,1,n) if(e[i].size()==1){
que.clear();
dfs(i,0);
reverse(ALL(que));
out.pb(que);
}
}
void work()
{
n=read();
For(i,1,n) e[i].clear(),g1[i].clear(),g2[i].clear(),vis[i]=0; out.clear();
For(i,1,n) {
bs[i].reset();
For(j,1,n) bs[i][j]=1;
}
For(i,2,n){
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
For(i,1,n) if(e[i].size()==1 && !vis[i]) {
vis[i]=1;
int u=i,pre=0;
// while(1){
// int v=0;
// for(int x:e[u]) if(x!=pre) v=x;
// if(!v||e[v].size()>2)break;
// vis[v]=1,pre=u,u=v;
// }
}
For(i,1,n){
for(int v:e[i]){
if(vis[i]||vis[v])g1[i].pb(v);
else g2[i].pb(v);
}
}
bool flag=0,qaq=0;
int cnt=0,fir=0;
For(i,1,n) if(!vis[i]){
qaq=1;
int sum=0;
for(int v:e[i]) sum+=(!vis[v]);
if(sum<=1){
++cnt; fir=i;
que.clear();
dfs(i,0);
reverse(ALL(que));
out.pb(que);
// for(int x:que) cout<<x<<" "; cout<<" que\n";
if(add(que)){
flag=1;
break;
}
}
}
if(cnt==1) {
que.clear();
dfs(fir,0);
reverse(ALL(que));
// for(int x:que) cout<<x<<" "; cout<<" Que\n";
out.pb(que);
flag|=add(que);
}
if(!qaq) my1(),flag=1;
// For(i,1,n) For(j,1,n) cout<<bs[i][j]<<" \n"[j==n];
assert(flag);
cout<<SZ(out)<<"\n";
for(auto it:out){
for(int x:it)cout<<x<<" ";
cout<<"\n";
}
}
signed main()
{
int T=read();
while(T--)work();
return 0;
}
/*
*/
详细
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 2ms
memory: 38820kb
input:
10 10 4 2 5 1 8 6 1 9 3 7 1 8 10 2 6 4 3 4 10 4 3 3 1 5 3 6 3 2 3 8 7 8 2 7 10 8 9 10 5 4 10 8 10 3 6 3 9 8 1 10 4 2 2 7 8 4 10 10 6 6 8 1 7 2 6 3 5 3 9 4 2 6 9 3 1 10 2 8 10 4 9 1 9 3 5 7 6 3 1 8 8 7 4 2 10 9 2 9 1 7 1 5 6 8 2 3 9 5 10 5 4 1 10 10 2 9 8 1 8 5 6 3 7 1 6 2 9 8 2 10 4 2 10 5 7 6 2 8 7...
output:
3 7 3 10 2 4 6 8 9 5 1 7 3 5 9 1 8 6 4 10 2 9 5 1 8 6 10 2 4 7 3 2 10 7 9 8 2 6 5 1 4 3 4 1 5 6 3 2 9 8 10 7 2 6 3 1 10 9 8 5 4 7 2 7 2 5 4 9 8 1 10 6 3 2 4 2 8 10 6 9 5 3 7 1 7 1 5 3 9 10 8 6 4 2 3 5 7 10 4 2 8 1 9 6 3 5 7 6 3 9 1 8 2 10 4 6 3 9 1 10 4 2 8 5 7 2 4 6 5 10 7 1 3 9 8 2 8 ...
result:
ok Answer correct!
Subtask #2:
score: 80
Accepted
Test #2:
score: 80
Accepted
time: 17ms
memory: 38940kb
input:
100 100 90 19 79 98 23 34 50 41 31 52 61 19 50 30 49 5 95 65 22 44 72 89 49 77 27 7 48 2 28 25 56 12 97 63 98 43 10 4 50 33 12 13 54 16 100 43 23 69 53 5 56 85 39 6 64 92 100 59 2 71 44 29 59 97 64 39 75 53 59 89 16 35 67 16 6 43 38 51 36 22 58 70 3 29 9 61 99 11 49 95 27 72 73 89 23 3 14 3 61 57 26...
output:
15 17 36 1 13 12 85 56 96 74 32 18 11 99 55 8 58 70 86 46 83 30 41 50 33 81 21 80 87 15 82 57 9 61 19 90 62 63 97 31 52 71 2 48 54 78 67 16 35 93 20 26 91 76 88 40 37 84 92 64 66 39 24 6 47 45 79 98 43 100 59 7 27 72 89 42 73 69 60 34 23 14 3 29 68 44 22 51 38 65 95 94 28 25 77 49 5 53 75 10 4 58 8...
result:
ok Answer correct!