codeforces #216前三題

果7發表於2013-12-06

前兩題就直接粘程式碼了。。


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



相關文章