QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661110 | #8333. Gift | czrq | WA | 1ms | 6896kb | C++17 | 1.6kb | 2024-10-20 14:44:53 | 2024-10-20 14:44:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+5;
vector<int> e[M];
bool h[M];
int du[M],d[M];
int chi[M],n,add;
int ans=0;
bool vis[M];
vector<int> hu;
void huan(){
queue<int> q;
for(int i=1;i<=n;i++){
if(du[i]==1) q.push(i);
vis[i]=1;
}
while(q.size()){
int u=q.front(); q.pop();
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(vis[v]) continue;
du[v]--;
if(du[v]==1) {
q.push(v); vis[v]=1;
}
}
}
for(int i=1;i<=n;i++){
if(du[i]>1) h[i]=1;
}
}
vector<int> d5;
void dfs(int u,int fa){
vis[u]=1;
hu.push_back(u);
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(!h[v]||vis[v]||v==fa) continue;
dfs(v,u);
//break;
}
}
int main(){
cin>>n; add=n; int u,v;
for(int i=1;i<=n;i++){
cin>>u>>v;
e[u].push_back(v);
e[v].push_back(u);
du[u]++; du[v]++;
d[u]++; d[v]++;
}
huan();
for(int i=1;i<=n;i++){
if(d[i]>=4) add--;
if(h[i]&&d[i]==5) d5.push_back(i);
}
if(d5.size()==2){
cout<<add<<endl;
return 0;
}
else if(d5.size()==1){
for(int i=0;i<e[d5[0]].size();i++){
int v=e[d5[0]][i];
if(h[v]){
if(du[i]==4) ans+=add+1;
else ans+=add;
}
}
cout<<ans<<endl;
return 0;
}
for(int i=1;i<=n;i++){
if(h[i]){
u=i; break;
}
}
dfs(u,0);
for(int i=0;i<hu.size()-1;i++){
int u=hu[i],v=hu[i+1];
int tmp=add;
if(d[u]==4) tmp+=1; if(d[v]==4) tmp+=1;
ans+=tmp;
}
u=hu[0],v=hu[hu.size()-1];
int tmp=add;
if(d[u]==4) tmp+=1; if(d[v]==4) tmp+=1;
ans+=tmp;
cout<<ans<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6896kb
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: 1ms
memory: 5916kb
input:
3 1 3 3 2 2 1
output:
3
result:
wrong answer 1st numbers differ - expected: '9', found: '3'