QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#793272 | #8333. Gift | Maxduan# | RE | 1ms | 5648kb | C++20 | 1.7kb | 2024-11-29 18:09:57 | 2024-11-29 18:09:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
vector<int>g[maxn];
int vis[maxn];
stack<int>st;
int du[maxn];
int cnt[maxn];
long long ans=0;
bool flag=0;
void dfs(int u,int fa){
if(flag)return;
//cout<<"u="<<u<<' '<<fa<<'\n';
if(vis[u]){
//cout<<"U="<<u<<'\n';
int tmp=u;
while(st.size()&&st.top()!=u){
int nxt=st.top();
st.pop();
cnt[du[tmp]]--,cnt[du[nxt]]--;
du[tmp]--,du[nxt]--;
cnt[du[tmp]]++,cnt[du[nxt]]++;
if(!cnt[5])ans+=cnt[3]+cnt[2]+cnt[1];
cnt[du[tmp]]--,cnt[du[nxt]]--;
du[tmp]++,du[nxt]++;
cnt[du[tmp]]++,cnt[du[nxt]]++;
tmp=nxt;
}
if(tmp!=u){
int nxt=u;
cnt[du[tmp]]--,cnt[du[nxt]]--;
du[tmp]--,du[nxt]--;
cnt[du[tmp]]++,cnt[du[nxt]]++;
if(!cnt[5])ans+=cnt[3]+cnt[2]+cnt[1];
cnt[du[tmp]]--,cnt[du[nxt]]--;
du[tmp]++,du[nxt]++;
cnt[du[tmp]]++,cnt[du[nxt]]++;
tmp=nxt;
}
flag=1;
return;
}
st.push(u);
vis[u]=1;
for(auto v:g[u]){
if(v==fa)continue;
dfs(v,u);
}
st.pop();
}
void solve(){
int n;cin>>n;
for(int i=1;i<=n;i++){
int u,v;cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
du[u]++;
du[v]++;
}
for(int i=1;i<=n;i++){
cnt[du[i]]++;
}
dfs(1,0);
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int TT=1;//cin>>TT;
while(TT--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5648kb
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: 1ms
memory: 3560kb
input:
3 1 3 3 2 2 1
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: -100
Runtime Error
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...