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

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 (1≤n≤2?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 (1≤x,y≤n1≤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 (1≤ai≤n1≤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

41 21 32 41 2 3 4

output

Yes

input

41 21 32 41 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 (2≤n≤2?105,1≤m≤2?1052≤n≤2?105,1≤m≤2?105, 1≤k<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 (1≤i≤m1≤i≤m) of the next mm lines contains two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠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 (1≤i≤m1≤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 22 31 21 31 4

output

Copy

0033

input

Copy

5 8 22 14 25 45 24 35 14 13 2

output

Copy

00033445

input

Copy

5 7 21 53 22 53 41 25 31 3

output

Copy

0000344

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]);
}

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

时间: 2024-10-01 00:29:22

Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D,E的相关文章

题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cstring> #include <stack> #include <set> using namespace std; int u[200010],v[200010],n,m,k,ans; bool isdel[200010],vis[200010]; set<int>

Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造

Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Description] ? 给你一个\(n\),构造一个\(n\times n\)的矩阵,使其满足任意一行,或一列的异或值相同.保证\(n\)能被\(4\)整除. [Solution] ? 可以发现,从\(0\)开始,每\(4\)个连续数的异或值为\(0\),所以可以很容易使得每一行的异或值等于\(0\). ? 列

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g

[Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated]-E. Kuroni and the Score Distribution(构造)

[Ozon Tech Challenge 2020 (Div.1 + Div.2, Rated]-E. Kuroni and the Score Distribution(构造) Kuroni is the coordinator of the next Mathforces round written by the "Proof by AC" team. All the preparation has been done, and he is discussing with the

Codeforces Round #621 (Div. 1 + Div. 2)

Codeforces Round #621 (Div. 1 + Div. 2) A. Cow and Haybales 贪心,移到第一堆的代价即为与第一堆的距离. #include <bits/stdc++.h> using namespace std; void solve(){ int n,d;cin>>n>>d; int sum;cin>>sum; for(int i=1;i<n;i++){ int t;cin>>t; if(d>

Codeforces Round #621 (Div. 1 + Div. 2)D dij(思维)

题:https://codeforces.com/contest/1307/problem/D 题意:给定无向图,n为点,m为边.在给个k,为特殊点的数目,题目要求在这些特殊点上连一条边,让新图最短路尽可能大,问新图最短路(1到n)是多少? 分析:因为题目保证连通且原本的图一定可以从1到n,我们假设原本的图最短路为ans: 易得虽然要求我们最大化最短路,但是加一条边只可能让答案不变或变小(因为一定要加这条边),不会使最短路变大: 假设我们在特殊点x和y之间加了边,要是新图的最短路有走这条边,那么

【JQuery】简单实现div结合select实现div触发select新增option,并通过键盘上下键选中option后追加到div中。

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+requ

DIV布局-DIV高度不同自动换行并对齐《转》

每个div框内容有多有少,要支持div高度自适应,还要添加的div自动追加,并且换行还要保持每行对齐. 刚开始的效果: 给出了完美解决方案: 效果: 因为要支持每个div可删除,删除后,后面的div自动补齐,所以用table不显示(除非想自虐的人可以试下) 最终就是css修改了一下就搞定了... 1 <html> 2 <head> 3 <style> 4 .test_area{ 5 width:100%; 6 background-color:#FFFFFF; 7 mi

xHTML+div布局:三个div,两边div宽度固定,中间div宽度自适应

xHTML+div经常考题:三个div,两边div宽度固定,中间div宽度自适应. 和大家分享一个实现方式: 1.html代码 1 <div class="dyleft">左栏固定宽度为200px</div> 2 <div class="dyright">右栏固定宽度为200px</div> 3 <div class="dycenter">中间自适应宽度</div> 2.cs