Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

CF260D Black and White Tree

$
0
0

因为保证有解,所以尽可能去构造就可以了。

每次在一个节点上连尽可能多的边。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100005;

struct Node
{
    int index, sum;
} black[MAXN], white[MAXN];
int blackNum, whiteNum;

int main()
{
    int n, color;
    while(~scanf("%d", &n))
    {
        blackNum = whiteNum = 0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d", &color);
            if(color)
            {
                black[blackNum].index = i;
                scanf("%d", &black[blackNum].sum);
                ++ blackNum;
            }
            else
            {
                white[whiteNum].index = i;
                scanf("%d", &white[whiteNum].sum);
                ++ whiteNum;
            }
        }
        for(int i=0,j=0;j<whiteNum;)
        {
            for(;black[i].sum>=0&&j<whiteNum;++j)
            {
                if(black[i].sum >= white[j].sum)
                {
                    printf("%d %d %d\n", black[i].index, white[j].index, white[j].sum);
                    black[i].sum -= white[j].sum;
                }
                else
                {
                    printf("%d %d %d\n", black[i].index, white[j].index, black[i].sum);
                    white[j].sum -= black[i].sum;
                    black[i].sum = -1;
                }
            }
            ++ i, -- j;
            for(;white[j].sum>=0&&i<blackNum;++i)
            {
                if(white[j].sum >= black[i].sum)
                {
                    printf("%d %d %d\n", white[j].index, black[i].index, black[i].sum);
                    white[j].sum -= black[i].sum;
                }
                else
                {
                    printf("%d %d %d\n", white[j].index, black[i].index, white[j].sum);
                    black[i].sum -= white[j].sum;
                    white[j].sum = -1;
                }
            }
            --i, ++ j;
        }
    }
    return 0;
}


作者:CyberZHG 发表于2012-12-29 21:26:00 原文链接
阅读:49 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles