日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

中石油训练赛 - Plan B(点双缩点+树形dp)

發(fā)布時(shí)間:2024/4/11 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中石油训练赛 - Plan B(点双缩点+树形dp) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目大意:給出一張 n 個(gè)點(diǎn) m 條邊的無(wú)向連通圖,現(xiàn)在有某些點(diǎn)被標(biāo)記了,問(wèn)能否通過(guò)刪除某個(gè)未被標(biāo)記的點(diǎn),使得刪除該點(diǎn)后的數(shù)個(gè)互不相交的連通塊中,至少存在一個(gè)聯(lián)通塊中不含有被標(biāo)記的點(diǎn)

題目分析:首先不難看出,被刪掉的點(diǎn)一定是割點(diǎn),所以我們可以直接用 tarjan 求出割點(diǎn),然后點(diǎn)雙縮一下點(diǎn),將整個(gè)圖縮成一棵樹(shù),在樹(shù)上dp即可,dp 的話對(duì)于點(diǎn) u 而言,只需要檢查一下其所有子樹(shù),以及其父節(jié)點(diǎn)上面的那棵子樹(shù)中,是否存在著不含有標(biāo)記點(diǎn)的子樹(shù)即可

代碼:
?

//#pragma GCC optimize(2) //#pragma GCC optimize("Ofast","inline","-ffast-math") //#pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;const int M=1e6+100;struct Egde {int to,next; }edge1[M],edge2[M];int head1[N],head2[N],low[N],dfn[N],c[N],Stack[N],new_id[N],dp[N],num,cnt,cnt1,cnt2,tot,root,top,n,m,b;bool cut[N],ban[N];vector<int>dcc[N];vector<int>ans;map<int,int>mp;void addedge1(int u,int v) {edge1[cnt1].to=v;edge1[cnt1].next=head1[u];head1[u]=cnt1++; }void addedge2(int u,int v) {edge2[cnt2].to=v;edge2[cnt2].next=head2[u];head2[u]=cnt2++; }void tarjan(int u) {dfn[u]=low[u]=++num;Stack[++top]=u;if(u==root&&head1[u]==-1){dcc[++cnt].push_back(u);return;}int flag=0;for(int i=head1[u];i!=-1;i=edge1[i].next){int v=edge1[i].to;if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]){flag++;if(u!=root||flag>1)cut[u]=true;cnt++;int x;do{x=Stack[top--];dcc[cnt].push_back(x);}while(x!=v);dcc[cnt].push_back(u);}}elselow[u]=min(low[u],dfn[v]);} }void solve() {for(int i=1;i<=n;i++)//找割點(diǎn)+縮點(diǎn) if(!dfn[i]){root=i;tarjan(i);} }void build()//縮點(diǎn)+連邊 {solve();num=cnt;for(int i=1;i<=n;i++)if(cut[i]){new_id[i]=++num;mp[num]=i;}for(int i=1;i<=cnt;i++)for(int j=0;j<dcc[i].size();j++){int x=dcc[i][j];if(cut[x]){addedge2(i,new_id[x]);addedge2(new_id[x],i);}elsec[x]=i;} }void dfs(int u,int fa) {bool flag=false;for(int i=head2[u];i!=-1;i=edge2[i].next){int v=edge2[i].to;if(v==fa)continue;dfs(v,u);if(!dp[v])flag=true;dp[u]+=dp[v];}if(mp.count(u)&&!ban[mp[u]]&&(flag||dp[u]==b))ans.push_back(mp[u]); }void init() {for(int i=0;i<N;i++)dcc[i].clear();cnt=cnt2=cnt1=num=tot=top=0;memset(head2,-1,sizeof(head2));memset(head1,-1,sizeof(head1));memset(low,0,sizeof(low));memset(dfn,0,sizeof(dfn));memset(cut,false,sizeof(cut));memset(c,0,sizeof(c));memset(Stack,0,sizeof(Stack));memset(new_id,0,sizeof(new_id));memset(dp,0,sizeof(dp));memset(ban,false,sizeof(ban));mp.clear();ans.clear(); }int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);init();scanf("%d%d%d",&n,&m,&b);while(m--){int u,v;scanf("%d%d",&u,&v);addedge1(u,v);addedge1(v,u);}build();for(int i=1;i<=b;i++){int x;scanf("%d",&x);ban[x]=true;if(cut[x])dp[new_id[x]]++;elsedp[c[x]]++;}dfs(1,-1);sort(ans.begin(),ans.end());printf("%d\n",ans.size());for(int num:ans)printf("%d ",num);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的中石油训练赛 - Plan B(点双缩点+树形dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。