1. 程式人生 > 其它 >Noip模擬8 2021.6.17

Noip模擬8 2021.6.17

T1 星際旅行

仔細一看,發現像一個尤拉路(簡稱一筆畫)。

滿足“可以一筆畫”的條件是:

1.所有點都有偶數條連邊;

2.有偶數個點連奇數條邊;

滿足以上兩個條件的任意一個即可一筆畫。

然而還要保證圖的聯通性。就是說如果有一個圖,有一些點是孤立的,按照題意也是可行的。但是如果影象是兩個不同的區域,每個區域的點互不連

這種情況就是假的,直接輸出零。

因為雙向邊,我們不妨把每條邊看作有兩條。那麼滿足題目情況有三種:

1.去掉任意兩個自環;

2.去掉一個自環和任意一條邊;

3.去掉連在一個點上的任意兩條邊;

 1 #include<bits/stdc++.h>
 2 #define int long long
 3
using namespace std; 4 const int NN=100005; 5 int n,m,cnt,ans,out[NN]; 6 bool vis[NN]; 7 struct SNOW{int to,next;}; SNOW e[NN<<1]; int r[NN<<1],tot; 8 inline void add(int x,int y){e[++tot]=(SNOW){y,r[x]}; r[x]=tot;} 9 inline void dfs(int x){ 10 vis[x]=true; 11 for(int i=r[x];i;i=e[i].next)
12 if(!vis[e[i].to]) dfs(e[i].to); 13 } 14 namespace WSN{ 15 inline int main(){ 16 scanf("%lld%lld",&n,&m); 17 for(int i=1,x,y;i<=m;i++){ 18 scanf("%lld%lld",&x,&y); 19 if(x==y) {cnt++;continue;} 20 add(x,y); add(y,x);
21 out[x]++; out[y]++; 22 } 23 ans+=cnt*(cnt-1)/2+cnt*tot/2; 24 for(int i=1;i<=n;i++) if(out[i]) {dfs(i);break;} 25 if(!tot) {cout<<0<<endl; return 0;} 26 for(int i=1;i<=n;i++) if(!vis[i] && out[i]){cout<<0<<endl; return 0;} 27 for(int i=1;i<=n;i++) ans+=out[i]*(out[i]-1)/2; 28 printf("%lld\n",ans); 29 return 0; 30 } 31 } 32 signed main(){return WSN::main();}
View Code

T2 砍樹