codeforces #216前三题

原创 2013年12月06日 00:22:48

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


题目地址: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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Codeforces Round #216 (Div. 2)部分题解

A. Valera and Plates 题型:小贪心 题意: 主角有两种干净的餐具,1号餐具m个,2号k个 他每天用一个盘子吃一种套餐,而且用了不洗,而是等到没盘子用了才会洗一个, ...

Codeforces Round #216 (Div. 2)---C. Valera and Elections

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

Codeforces Round #216_div2_D.Valera and Fools

近些日子以来总觉得编程水平没怎么提升,这学期开始虽然每周有四场的队内训练赛,但几个月下来感觉收获不大。想想,还是以刷题的方式来学习吧。每天刷个一两题,然后写写题解。一味的打比赛打比赛被逼的很紧但是却没...

Codeforces Round #216 (Div. 2)

500pt: 题目连接:http://codeforces.com/problemset/problem/369/A 思路:直接贪心,注意一次只能洗一只碗,一开始以为洗一次能把所有都洗好。。。 ...

Codeforces Round #216 (Div. 2)

A.分别记录两种食物的数量,先统计第一种洗盘数,后统计第二种洗餐具数。 #include #include #include #include #include #include #in...

Codeforces Round #216 (diy.2) C. Valera and Elections

题目链接:点击打开链接 题意:给一棵树,N-1条边的权值可以是1和2。要找包含权值为2的边的路径有几条,并            记录这条路径中距离点1最远的点。 分析: 就是树形dp,或者说是树上的...

Codeforces Round #216 (Div. 2) E. Valera and Queries (树状数组)

题目地址:http://codeforces.com/contest/369/problem/E 看完题目,很明显是离散+树状数组的思路,然而并没有想到怎么离散。这题的解法实在巧妙。 这题要求的是...

Codeforces Round #216_div2_E.Valera and Queries

转载注明出处   http://blog.csdn.net/moedane     传送门 http://codeforces.com/contest/369/problem/E   题意 给出n条线...

【打CF,学算法——三星级】CodeForces 216B Forming Teams (图论)

【CF简介】 提交链接:CF 216B 题面: B. Forming Teams time limit per test 2 seconds memor...

Codeforces Round #216 (Div. 2)

Codeforces Round #216 (Div. 2)
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)