码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E

时间:2018-09-04 23:35:49      阅读:453      评论:0      收藏:0      [点我收藏+]

标签:output   single   ace   RKE   else   cti   imu   you   while   

D. Valid BFS?
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The BFS algorithm is defined as follows.

  1. Consider an undirected graph with vertices numbered from 11 to nn. Initialize qq as a new queue containing only vertex 11, mark the vertex 11 as used.
  2. Extract a vertex vv from the head of the queue qq.
  3. Print the index of vertex vv.
  4. Iterate in arbitrary order through all such vertices uu that uu is a neighbor of vv and is not marked yet as used. Mark the vertex uu as used and insert it into the tail of the queue qq.
  5. If the queue is not empty, continue from step 2.
  6. Otherwise finish.

Since the order of choosing neighbors of each vertex can vary, it turns out that there may be multiple sequences which BFS can print.

In this problem you need to check whether a given sequence corresponds to some valid BFS traversal of the given tree starting from vertex 11. The tree is an undirected graph, such that there is exactly one simple path between any two vertices.

Input

The first line contains a single integer nn (1n2?1051≤n≤2?105) which denotes the number of nodes in the tree.

The following n?1n?1 lines describe the edges of the tree. Each of them contains two integers xx and yy (1x,yn1≤x,y≤n) — the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree.

The last line contains nn distinct integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n) — the sequence to check.

Output

Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise.

You can print each letter in any case (upper or lower).

Examples
input
Copy
4
1 2
1 3
2 4
1 2 3 4
output
Yes
input
4
1 2
1 3
2 4
1 2 4 3
output
No
Note

Both sample tests have the same tree in them.

In this tree, there are two valid BFS orderings:

  • 1,2,3,41,2,3,4,
  • 1,3,2,41,3,2,4.

The ordering 1,2,4,31,2,4,3 doesn‘t correspond to any valid BFS order.

题意  给定一棵树,在给定一个序列,问是不是以1为根的BFS序。

解析  BFS序的特点就是 安层次的所以  i 的儿子是连续的 直接暴力判断当前的段是不是都是 i 的儿子。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=2e5+10;
typedef long long ll;
vector<int> g[maxn];
map<pair<int,int>,int> ma;
int a[maxn];
int b[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
        ma[mp(u,v)]=1;
        ma[mp(v,u)]=1;
    }
    for(int i=1;i<=n;i++)
        cin>>a[i];
    if(a[1]!=1)
    {
        cout<<"No"<<endl;
        return 0;
    }
    int flag=1,before=1;
    for(int i=1;i<n;i++)
    {
        int num=0;
        for(int j=0;j<g[a[i]].size();j++)
            if(b[g[a[i]][j]]==0)num++;
        //cout<<i<<" "<<num<<endl;
        for(int j=0;j<num;j++)
        {
            if(before+j+1>n)
                break;
           // cout<<a[i+j+1]<<" j"<<j<<endl;
            if(ma[mp(a[i],a[before+j+1])]!=1)
            {
                flag=0;
                break;
            }
            //else
              //  b[a[i+j+1]]=1;
        }
        before=before+num;
        //cout<<a[i]<<" "<<flag<<endl;
        b[a[i]]=1;
        if(flag==0)
            break;
    }
    if(flag)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
}

 

E. Trips
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn persons who initially don‘t know each other. On each morning, two of them, who were not friends before, become friends.

We want to plan a trip for every evening of mm days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:

  • Either this person does not go on the trip,
  • Or at least kk of his friends also go on the trip.

Note that the friendship is not transitive. That is, if aa and bb are friends and bb and cc are friends, it does not necessarily imply that aa and cc are friends.

For each day, find the maximum number of people that can go on the trip on that day.

Input

The first line contains three integers nn, mm, and kk (2n2?105,1m2?1052≤n≤2?105,1≤m≤2?105, 1k<n1≤k<n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.

The ii-th (1im1≤i≤m) of the next mm lines contains two integers xx and yy (1x,yn1≤x,y≤n, xyx≠y), meaning that persons xx and yy become friends on the morning of day ii. It is guaranteed that xx and yy were not friends before.

Output

Print exactly mm lines, where the ii-th of them (1im1≤i≤m) contains the maximum number of people that can go on the trip on the evening of the day ii.

Examples
input
Copy
4 4 2
2 3
1 2
1 3
1 4
output
Copy
0
0
3
3
input
Copy
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
output
Copy
0
0
0
3
3
4
4
5
input
Copy
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
output
Copy
0
0
0
0
3
4
4
Note

In the first example,

  • 1,2,31,2,3 can go on day 33 and 44.

In the second example,

  • 2,4,52,4,5 can go on day 44 and 55.
  • 1,2,4,51,2,4,5 can go on day 66 and 77.
  • 1,2,3,4,51,2,3,4,5 can go on day 88.

In the third example,

  • 1,2,51,2,5 can go on day 55.
  • 1,2,3,51,2,3,5 can go on day 66 and 77.

解析 我们离线处理这个问题,先把每个点的入度和编号用pair保存起来 扔到set里面 ,每次把度数小于k的点删掉,与它相邻的点 j 度数减1 。把原来在set里的pair<du[j],j>删掉,再插入新的点

直到 set里的点的度数都大于k。set的size就是当前的答案。删掉当前的边,重复上面的操作。

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
const int maxn=2e5+10;
typedef long long ll;
typedef pair<int,int> pii;
int du[maxn];
set<int> g[maxn];
int b1[maxn],b2[maxn],ans[maxn],vis[maxn];
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    set<pii> s;
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        b1[i]=x,b2[i]=y;
        du[x]++,du[y]++;
        g[x].insert(y),g[y].insert(x);
    }
    for(int i=1;i<=n;i++)
        s.insert(mp(du[i],i));
    for(int i=m-1;i>=0;i--)
    {
        while(s.size()>=1)
        {
            auto itt=(*s.begin());
            if(itt.fi>=k)break;
            for(auto it=g[itt.se].begin();it!=g[itt.se].end();it++)
            {
                int u=*it;
                if(vis[u]==1)continue;
                s.erase(mp(du[u],u));
                du[u]--;
                s.insert(mp(du[u],u));
                g[u].erase(itt.se);
            }
            s.erase(itt);
            vis[itt.se]=1;
        }
        ans[i]=s.size();
        if(vis[b1[i]]==0&&vis[b2[i]]==0)
        {
            s.erase(mp(du[b1[i]],b1[i]));
            du[b1[i]]--;
            s.insert(mp(du[b1[i]],b1[i]));
            g[b1[i]].erase(b2[i]);
            s.erase(mp(du[b2[i]],b2[i]));
            du[b2[i]]--;
            s.insert(mp(du[b2[i]],b2[i]));
            g[b2[i]].erase(b1[i]);
        }
    }
    for(int i=0;i<m;i++)
        printf("%d\n",ans[i]);
}

 

Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E

标签:output   single   ace   RKE   else   cti   imu   you   while   

原文地址:https://www.cnblogs.com/stranger-/p/9588974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!