关闭

codeforces #216前三题

标签: codeforces
1381人阅读 评论(2) 收藏 举报
分类:

前两题就直接粘代码了。。


题目地址:A. Valera and Plates

AC代码:

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    int n,m,k,i;
    int t;
    while(cin>>n>>m>>k)
    {
        int ans=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&t);
            if(t==1)
            {
                if(m)
                {
                    m--;
                }
                else
                {
                    ans++;
                }
            }
            else
            {
                if(k)
                {
                    k--;
                }
                else if(m)
                {
                    m--;
                }
                else
                    ans++;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}


题目地址:B. Valera and Contest

AC代码:

#include<iostream>
#include<cstdio>
using namespace std;

int a[1002];

int main()
{
    int n,k,l,r,sall,sk,i,j;
    int tmp,tn;
    while(cin>>n>>k>>l>>r>>sall>>sk)
    {
        tn=n-k;
        for(i=1; i<=n; i++)
        {
            if(sk==0)
            {
                //cout<<i<<endl;
                for(j=i; j<=n; j++)
                {
                    //cout<<sall<<" "<<tn<<endl;
                    tmp=sall/tn;
                    if(sall%tn) tmp++;
                    //cout<<tmp<<endl;
                    a[j]=tmp;
                    sall-=tmp;
                    tn--;
                }
                break;
            }
            tmp=sk/k;
            if(sk%k) tmp++;
            a[i]=tmp;
            sk-=tmp;
            sall-=tmp;
            k--;
        }

        cout<<a[1];
        for(i=2; i<=n; i++)
            cout<<" "<<a[i];
        cout<<endl;
    }
    return 0;
}



C. Valera and Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The city Valera lives in is going to hold elections to the city Parliament.

The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.

There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.

Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.

Input

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.

Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xiyiti (1 ≤ xi, yi ≤ n1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if tiequals to two, then the i-th road is the problem road.

It's guaranteed that the graph structure of the city is a tree.

Output

In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.

Sample test(s)
input
5
1 2 2
2 3 2
3 4 2
4 5 2
output
1
5 
input
5
1 2 1
2 3 2
2 4 1
4 5 1
output
1
3 
input
5
1 2 2
1 3 2
1 4 2
1 5 2
output
4
5 4 3 2 



题目不难,当时看见大概有六百+的人过了这题的样例,但是自己想不到怎么样来建图。。。题目意思不难理解,就是说有n个顶点,1,2.....n这样的定点,然后有n-1条边,确保是一颗树。边的值可以是1,代表这条路没问题,是2代表有问题。需要人来修,现在选人数最少的人来修所有为2的边。一个人可以修到1过程中走的路上的所有边。于是问他抽象出来就是找离1最远的点并且边是2这样的点。一直苦于不知如何建图。。。。


解题思路:

1)用vector数组保存每个节点的子节点;

2)深搜,找到每个节点的父节点,从而建立一棵树;

3)每个结点往上面找,如果这个点需要修路,把他自己设置为1,把他所有的父亲祖宗结点都变为0,并设置为访问过,访问过的就不再访问了。


题目地址:C. Valera and Elections


AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn=100005;

int visi[maxn];  //dfs建树的时候用 从叶子向根遍历的时候用
int par[maxn];
vector <int> mq[maxn];
int p[maxn];

struct node
{
    int a;
    int b;
    int val;
};
node road[maxn];

void dfs(int p)
{
    for(int i=0;i<mq[p].size();i++)
    {
        int cur=mq[p][i];
        if(!visi[cur])
        {
            visi[cur]=1;
            par[cur]=p;
            dfs(cur);
        }
    }
}

int main()
{
    int n,i;
    int l,r,v;
    while(cin>>n)
    {
        memset(visi,0,sizeof(visi));
        for(i=1;i<=n;i++)
            mq[i].clear();
        for(i=0; i<n-1; i++)     //先把所有结点的关系建立起来
        {
            scanf("%d%d%d",&l,&r,&v);
            road[i].a=l,road[i].b=r,road[i].val=v;
            mq[l].push_back(r);
            mq[r].push_back(l);
        }

        visi[1]=1;
        dfs(1);  //由1为根结点建树

        memset(visi,0,sizeof(visi));
        memset(p,0,sizeof(p));
        for(i=0;i<n-1;i++)
        {
            l=road[i].a,r=road[i].b;
            v=road[i].val;
            int t;   //t为孩子结点
            if(v==2)
            {
                if(par[l]==r)
                    t=l;
                else
                    t=r;

                if(!visi[t])
                {
                    p[t]=1;
                    int x=par[t];
                    while(x!=1)
                    {
                        if(visi[x]) break;
                        visi[x]=1;
                        p[x]=0;
                        x=par[x];
                    }
                }
            }
        }

        int ans=0;
        for(i=1;i<=n;i++)
            if(p[i]==1)
                ans++;
        cout<<ans<<endl;

        int flag=0;
        for(i=n;i>=1;i--)
            if(p[i]==1)
            {
                if(flag==0)
                {
                    flag=1;
                    printf("%d",i);
                }
                else
                    printf(" %d",i);
            }
        cout<<endl;
    }
    return 0;
}



0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Codeforces 375D 数据结构(好题中的好题, 4解)

题目链接:http://codeforces.com/problemset/problem/375/D 全部代码: 题意:给你一棵树n个点,m次询问(n=100000,m=100000),每个节点有一...
  • c3568
  • c3568
  • 2014-03-25 19:38
  • 1427

CodeForces 171F(千古神题。。)

CF神题
  • sky_miange
  • sky_miange
  • 2015-02-12 19:01
  • 608

codeforces 627 problem A 好题呀,感觉学到了挺多的 亦或

A. XOR Equation time limit per test 2 seconds memory limit per test 256 megabytes input standa...
  • zcj5027
  • zcj5027
  • 2016-03-24 21:11
  • 695

codeforces Line 7C (拓展欧几里德+思想 模板) 好题

C. Line Description A line on the plane is described by an equation Ax + By + C = 0. You are to f...
  • yanghui07216
  • yanghui07216
  • 2015-10-24 21:04
  • 382

Codeforces 798D Mike and distribution【思维+贪心】好题!好题!感觉智商受到了一万点伤害= =

D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes ...
  • mengxiang000000
  • mengxiang000000
  • 2017-05-03 21:19
  • 404

概率dp入门的简单题(hdu 4405,牡丹江,codeforces)

最近学习了一下概率dp,感觉没有想象中的那么难。主要还是状态的转移方程的构建。 对于”求概率正推,求期望反推“这句话有了初步的理解。期望的意思是说你现在处在一个状态,还需要几步到达最终状态,这是一个期...
  • NK_test
  • NK_test
  • 2015-05-07 23:58
  • 725

Codeforces好题专栏(前三道)

为了上分,博主打算开一篇博客来记录下前三道遇到的好题.
  • qq_34921856
  • qq_34921856
  • 2017-10-09 08:46
  • 147

codeforces 237/A 一道水题的感悟。。。。

http://codeforces.com/problemset/problem/237/A Valera runs a 24/7 fast food cafe. He magically ...
  • u013573047
  • u013573047
  • 2014-04-08 17:57
  • 689

LCT模板及详细讲解

Link cut tree 从一个例子引入: 传送门 此题有两种解法,这里只提LCT的方法。 一棵树上有n个节点,有3个操作: 1、将节点u的权值改为t。 2、询问从点u到点v的路径上的节点的最大权...
  • Leo_Nasir
  • Leo_Nasir
  • 2017-09-08 19:35
  • 119

大坑!Codeforce DP题总结(持续更新)

个人非常喜欢做DP题,因为DP题有着特殊的数学美感持续不断吸引着我
  • rrtyui
  • rrtyui
  • 2014-09-22 23:25
  • 2144
    个人资料
    • 访问:353709次
    • 积分:6179
    • 等级:
    • 排名:第4683名
    • 原创:276篇
    • 转载:3篇
    • 译文:0篇
    • 评论:100条
    联系方式
    Emai1: 185986777@qq.com
    Emai2: putao0124@gmail.com
    友情链接