QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#668288 | #8333. Gift | AI80 | WA | 9ms | 5724kb | C++20 | 2.9kb | 2024-10-23 13:21:01 | 2024-10-23 13:21:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lowbit(x) (x & (-x))
typedef long long ll;
typedef pair<int,int> pii;
mt19937 rnd(time(0));
const int N = 1e7 + 7;
const int mod = 1e9 + 9;
int pre[N] , n;
vector<int> e[N];
bool vis[N];
void find() {
for(int i = 1;i <= n;i++) {
if(e[i].size() == 1) {
queue<int> q,qt;
q.push(i);
pre[i] = 0;
bool f = 0;
while(!q.empty()) {
if(f) break;
int u = q.front();
q.pop();
for(auto v : e[u]) {
if(v != pre[u]) {
if(!pre[v]) {
pre[v] = u;
q.push(v);
}
else {
qt.push(v); //让较长的点先入队,方便后续返回,较长的点一定是被找到的
qt.push(u);
f = 1;
break;
}
}
}
}
while(!qt.empty()) {
int u = qt.front();
qt.pop();
if(vis[u]) break;
vis[u] = 1;
qt.push(pre[u]);
}
return ;
}
}
for(int i = 1;i <= n;i++) vis[i] = 1;
return ;
}
void solve() {
cin >> n;
for(int i = 1;i <= n;i++) {
int u , v; cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
find();
vector<int> vv;
for(int i = 1;i <= n;i++) {
if(vis[i]) vv.push_back(i);
}
int res = 0,ans = 0;
for(int i = 1;i <= n;i++) {
if(e[i].size() <= 3) res++;
}
for(int i = 0;i < vv.size();i++) {
if(e[i].size() == 5) {
for(auto x : e[i]) {
if(vis[x]) {
if(e[x].size() == 5) {
cout << res << "\n";
return ;
}
else ans += res + (e[x].size() == 4);
}
}
cout << ans << "\n";
return ;
}
}
for(int i = 0;i < vv.size();i++) {
if(e[i].size() == 4) {
for(auto x : e[i]) {
if(vis[x]) {
if(e[i].size() == 4) ans += res + 2;
else ans += res + 1;
}
}
}
else {
for(auto x : e[i]) {
if(vis[x]) {
if(e[i].size() == 4) ans += res + 1;
else ans += res;
}
}
}
}
cout << ans / 2 << "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 5724kb
input:
6 1 2 1 3 1 4 1 5 1 6 2 3
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: -100
Wrong Answer
time: 9ms
memory: 5688kb
input:
3 1 3 3 2 2 1
output:
6
result:
wrong answer 1st numbers differ - expected: '9', found: '6'