QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#713365 | #8333. Gift | StoneXie# | TL | 2ms | 12784kb | C++17 | 2.2kb | 2024-11-05 19:08:56 | 2024-11-05 19:08:58 |
Judging History
answer
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define unmap unordered_map
#define unset unordered_set
#define MAXQ priority_queue
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define frep(i,a,b) for(int i=a;i<(b);++i)
using namespace std;
template<typename T> using MINQ=priority_queue<T,vector<T>,greater<T> >;
using pii=pair<int,int>;
using vi=vector<int>;
using vii=vector<vi>;
const int MAXN = 200010;
int n,cnt[MAXN],deg[MAXN];
int vis[MAXN];
vector<int> nxt[MAXN];
int point;
bool flag;
vector<int> g;
void dfs(int p, int fa) {
if(vis[p]) {
point = p;
flag = true;
return;
}
vis[p] = true;
for(auto x : nxt[p]) {
if(x == fa) continue;
dfs(x, p);
if(flag) {
g.push_back(p);
if(p == point) {
flag = false;
}
return;
}
}
vis[p] = false;
return;
}
void add(int i) {
cerr<<"add"<<i<<" "<<deg[i]<<endl;
cnt[deg[i]]--;
deg[i]++;
cnt[deg[i]]++;
}
void del(int i) {
cerr<<"del"<<i<<" "<<deg[i]<<endl;
cnt[deg[i]]--;
deg[i]--;
cnt[deg[i]]++;
}
void solve(){
int n;
cin >> n;
rep(i, 1, n) {
int x, y;
cin >> x >> y;
nxt[x].push_back(y);
nxt[y].push_back(x);
}
dfs(1, 0);
// for(auto x : g) cout << x << " ";
// cout<<endl;
for(int i=1;i<=n;i++) {
deg[i]=nxt[i].size();
cnt[nxt[i].size()]++;
}
int ans=0;
for(int i=0;i<g.size();i++) {
int u=g[i],v=g[(i+1)%g.size()];
del(u);del(v);
for(int j=1;j<=n;j++) cerr<<cnt[j]<<" ";
cerr<<endl;
if(cnt[1]+cnt[2]+cnt[3]+cnt[4]==n) {
ans+=cnt[1]+cnt[2]+cnt[3];
}
// cerr<<cnt[1]+cnt[2]+cnt[2]+cnt[3]+cnt[4]<<endl;
add(u);add(v);
}
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
int _t=1;
// cin>>_t;
// cout<<fixed<<setprecision(20);
for(int i=1;i<=_t;i++){
//cout<<"Case "<<i<<": ";
solve();
}
return 0;
}
/*
6
1 2
1 3
1 4
1 5
1 6
2 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11044kb
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: 0
Accepted
time: 0ms
memory: 12784kb
input:
3 1 3 3 2 2 1
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: -100
Time Limit Exceeded
input:
2332 1648 909 1676 2122 1644 1981 1106 1131 1785 239 223 618 335 1662 424 1775 889 1684 1589 52 1406 1747 1600 302 790 2056 1742 464 1706 541 1145 779 2316 833 1645 1439 859 438 1337 136 746 1565 436 1730 2079 2145 1583 1940 917 1549 1863 507 1266 367 1890 2230 13 2113 492 2109 120 1122 815 1111 134...