QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#592779 | #2515. Graph Cards | CSQ | AC ✓ | 19083ms | 85212kb | C++23 | 2.9kb | 2024-09-27 03:39:41 | 2024-09-27 03:39:42 |
Judging History
answer
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
typedef unsigned long long int ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int MAXN = 1e6+1;
vector<int>adj[MAXN];
int par[MAXN],vis[MAXN],cycle[MAXN];
vector<int>cyc;
vector<int> min_cyclic_string(vector<int>s) {
int n = sz(s);
for(int i=0;i<n;i++)s.push_back(s[i]);
n*=2;
int i = 0, ans = 0;
while (i < n / 2) {
ans = i;
int j = i + 1, k = i;
while (j < n && s[k] <= s[j]) {
if (s[k] < s[j])
k = i;
else
k++;
j++;
}
while (i <= k)
i += j - k;
}
return vector<int>(s.begin()+ans,s.begin()+ans+n/2);
}
bool dfs(int v,int u){
vis[v] = 1;
for(int x:adj[v]){
if(x==u)continue;
if(vis[x]){
int cur = v;
while(true){
cyc.push_back(cur);
cycle[cur] = 1;
cur = par[cur];
if(cur == x)break;
}
cyc.push_back(x);
cycle[x] = 1;
return 1;
}else{
par[x] = v;
if(dfs(x,v))return 1;
}
}
return 0;
}
map<vector<int>,int>seen;
int lab[MAXN];
int ccnt = 0;
void dfs2(int v,int u){
//cout<<v<<'\n';
vector<int>h;
for(int x:adj[v]){
if(x==u || cycle[x])continue;
dfs2(x,v);
h.push_back(lab[x]);
}
sort(all(h));
if(seen.find(h) == seen.end())seen[h] = ccnt++;
lab[v] = seen[h];
}
vector<int>generate(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int v,u;
cin>>v>>u;
adj[v].push_back(u);
adj[u].push_back(v);
}
dfs(1,0);
for(int x:cyc)dfs2(x,0);
vector<int>ans;
for(int x:cyc)ans.push_back(lab[x]);
vector<int>tmp = ans;
reverse(all(tmp));
tmp = min_cyclic_string(tmp);
ans = min_cyclic_string(ans);
for(int i=0;i<sz(ans);i++){
if(tmp[i] < ans[i]){
ans = tmp;
break;
}else if(ans[i] < tmp[i])break;
}
for(int i=0;i<=n;i++){
adj[i].clear();
cycle[i] = 0;
lab[i] = 0;
vis[i] = 0;
par[i] = 0;
}
cyc.clear();
return ans;
}
map<vector<int>,int>iso;
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int t;
cin>>t;
while(t--){
seen.clear();
iso.clear();
ccnt = 0;
int k;
cin>>k;
while(k--){
vector<int>v = generate();
//for(int x:v)cout<<x<<" ";
//cout<<'\n';
iso[v] = 1;
}
cout<<sz(iso)<<'\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9740kb
input:
1 2 4 1 2 2 3 3 1 1 4 4 1 2 2 3 3 1 2 4
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 2ms
memory: 9736kb
input:
2 2 4 1 2 2 3 3 1 1 4 5 1 2 2 3 3 1 2 4 2 5 3 9 1 2 2 5 5 7 7 6 6 3 3 1 2 4 7 9 9 8 9 1 4 4 2 2 3 3 5 5 7 7 6 6 4 7 8 8 9 9 1 2 2 5 5 4 4 1 4 7 7 8 8 6 8 9 5 3
output:
2 2
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 9458ms
memory: 32672kb
input:
30 332526 3 3 2 3 1 2 1 3 2 3 3 1 1 2 3 1 3 1 2 3 2 3 3 2 2 1 1 3 3 2 1 2 3 1 3 3 1 3 1 2 3 2 3 1 2 2 3 3 1 3 2 1 2 3 1 3 3 3 1 3 2 1 2 3 1 3 1 2 3 2 3 1 3 3 2 2 1 3 1 2 2 3 3 1 3 3 1 3 2 1 2 3 2 1 2 3 1 3 3 3 2 2 1 1 3 3 1 2 1 3 2 3 3 1 2 1 3 2 3 3 2 3 2 1 3 1 3 1 2 1 3 2 3 3 3 1 1 2 2 3 3 1 3 1 2 ...
output:
1 4 5 89 93 242 628 1575 3401 5703 7718 8888 9258 9355 9356 9203 9211 89 239 648 1739 4541 10352 19037 27089 31567 33030 33068 32480 31503
result:
ok 30 lines
Test #4:
score: 0
Accepted
time: 6898ms
memory: 19776kb
input:
30 333333 3 2 1 2 3 3 1 3 1 2 3 2 3 1 3 3 2 1 3 1 2 3 2 3 2 1 3 1 3 2 3 2 1 1 3 3 1 2 3 2 3 1 3 3 1 2 1 3 2 3 1 3 1 2 3 2 3 1 3 2 3 1 2 3 2 1 3 2 3 1 3 2 3 1 2 1 3 3 3 1 1 2 3 2 3 3 2 3 1 1 2 3 2 1 3 1 3 2 3 3 2 1 2 3 1 3 3 2 1 2 1 3 3 2 3 2 1 1 3 3 1 2 3 2 3 1 3 3 1 1 2 3 2 3 1 3 1 2 2 3 3 1 2 3 2 ...
output:
1 2 5 13 33 89 240 653 1771 4753 11868 24982 40524 51128 54690 54119 52143 49848 47551 45441 43471 41664 40000 38461 37037 35714 34482 33333 32258 31250
result:
ok 30 lines
Test #5:
score: 0
Accepted
time: 19083ms
memory: 85212kb
input:
30 2 500000 12446 494050 179161 216388 282442 232792 428247 130742 87062 493860 276791 469755 342468 58973 93535 429405 5662 112197 121541 62083 28611 427877 376918 349780 372239 58010 457751 308686 448844 473714 151350 480692 152372 415617 311966 298916 302690 200753 75481 396263 318635 79619 39930...
output:
1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2
result:
ok 30 lines
Test #6:
score: 0
Accepted
time: 134ms
memory: 47728kb
input:
1 4 240000 1 2 2 3 3 4 4 7 1 5 2 6 7 8 8 9 9 10 10 13 7 11 8 12 13 14 14 15 15 16 16 19 13 17 14 18 19 20 20 21 21 22 22 25 19 23 20 24 25 26 26 27 27 28 28 31 25 29 26 30 31 32 32 33 33 34 34 37 31 35 32 36 37 38 38 39 39 40 40 43 37 41 38 42 43 44 44 45 45 46 46 49 43 47 44 48 49 50 50 51 51 52 52...
output:
2
result:
ok single line: '2'