Cf 1027f session in BSU (query set + tree structure)

Source: Internet
Author: User

You can take the $ I $ test on day $ AI or day $ Bi $. You can take one test at most every day, the earliest end time of all tests.

Due to the large number of days, discretization is required.

Abstract The problem into a tree. Each vertex can be "allocated" at most. Now we need to delete the vertex.

Drawing and drawing can be found

If a Unicom block is a tree, you can delete at most one vertex.

If a Unicom block is a single ring tree (n vertices and n edges), no point can be deleted.

If the number of connected block edges is greater than the number of points, you will find that you cannot allocate only one edge for each vertex. invalid value: Output-1

Determine whether the decision tree is a single-ring tree. Find the sum of degrees in the inpoint of a connected block/2 and compare the points.

Check the set to maintain the connected block.

Note that the number of points is $2*10 ^ 6 $ instead of $1*10 ^ 6 $.

 1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N 2000100 6 #define maxn 100000 7 #define ll long long 8 #define mod 1000000007 9 #define iset multiset<node>::iterator10 using namespace std;11 //re12 int gint()13 {14     int ret=0,fh=1;char c=getchar();15     while(c<‘0‘||c>‘9‘){if(c==‘-‘)fh=-1;c=getchar();}16     while(c>=‘0‘&c<=‘9‘){ret=ret*10+c-‘0‘;c=getchar();}17     return ret*fh;18 }19 int n,m,K,ma,cte,num;20 int head[N],inc[N],id[N*2],vis[N],fa[N],sum[N],sz[N],typ[N];21 struct E{int x,y;}e[N];22 struct Edge{int to,nxt,val;}edge[N*2];23 void ae(int u,int v){24     cte++;edge[cte].to=v,inc[v]++;25     edge[cte].nxt=head[u],head[u]=cte;}26 int find_fa(int x){27     int y=x,pre;while(fa[y]!=y){y=fa[y];}28     while(fa[x]!=y){pre=fa[x],fa[x]=y,x=pre;}29     return y;30 }31 int dfs(int u)32 {33     vis[u]=1;int ans=1;34     for(int j=head[u];j;j=edge[j].nxt){35         int v=edge[j].to;36         if(vis[v]) continue;37         fa[v]=u,ans+=dfs(v),sz[u]+=sz[v];38     }sz[u]+=inc[u];return ans;39 }40 41 int main()42 {43     scanf("%d",&n);44     int x,y,z,fx;45     for(int i=1;i<=n;i++)46         e[i].x=gint(),e[i].y=gint(),47         id[++num]=e[i].x,id[++num]=e[i].y;48     sort(id+1,id+num+1);49     num=unique(id+1,id+num+1)-(id+1);50     for(int i=1;i<=n;i++){51         x=lower_bound(id+1,id+num+1,e[i].x)-id;52         y=lower_bound(id+1,id+num+1,e[i].y)-id;53         ae(x,y),ae(y,x);54     }int tot;55     for(int i=1;i<=num;i++) fa[i]=i;56     for(int i=1;i<=num;i++)57         if(!vis[i]){58             sum[i]=dfs(i);59             if(sum[i]==(sz[i]+2)/2)60                 typ[i]=1;61             else if(sum[i]==(sz[i]/2))62                 typ[i]=0;63             else{printf("-1\n");return 0;}64         }65     int ans=num;66     for(int i=num;i>=1;i--){67         fx=find_fa(i);68         if(typ[fx]==1) {typ[fx]=0;ans=i-1;}69         else break;70     }71     printf("%d\n",id[ans]);72     return 0;73 }

 

Cf 1027f session in BSU (query set + tree structure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.