图论+DFS——Bubble Cup 9 - Finals [Online Mirror] E

58 篇文章 0 订阅
12 篇文章 0 订阅
  • 题目链接:
    http://codeforces.com/contest/717/problem/E

  • 参考链接:
    http://codeforces.com/contest/717/submission/20538605

  • 分析:
    给出n个节点,和n个节点的颜色 1:黑色, -1: 粉红。再给出n-1条边,从1出发,每经过一个节点,节点的颜色将会变成另外一种。求一条路径,将所有的节点都变成黑色。

  • 题解:
    可以直接从根节点开始DFS,每条分支都走一遍,直到都走成黑色再停止

  • 参考代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 200005
#define MaxM MaxN*2
#define INF 0x3f3f3f3f
#define bug cout<<88888888<<endl;

int n;
vector <int> vec[MaxN];

int color[MaxN];

void DFS(int x=0, int p=-1)
{
    printf("%d ", x+1);
    color[x] *= -1;
    for(int i=0;i<vec[x].size();i++)
    {
        if( vec[x][i] == p) continue;

        DFS(vec[x][i], x);

        if(x==0 && i == vec[x].size()-1 && color[vec[x][i]] == 1 && color[x] == 1) 
            return;

        printf("%d ", x+1);
        color[x] *= -1;

        if(color[vec[x][i]] == -1)
        {
            printf("%d ", vec[x][i]+1 );
            color[vec[x][i]] *= -1;

            if(x == 0 && i == vec[x].size()-1 && color[vec[x][i]] == 1 && color[x] == 1)
                return;

            printf("%d ", x+1);
            color[x] *= -1;
        }
    }
}

int main()
{
    while(~scanf("%d", &n))
    {   
        for(int i=0;i<n;i++)
        {
            cin >> color[i];
        }
        color[0]*=-1;
        for(int i=1;i<n;i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            u--,v--;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        DFS();
    }
    //system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a Model class with the following UML specification: +----------------------------------------------+ | Model | +----------------------------------------------+ | - score: int | | - bubbles: ArrayList<IShape> | +----------------------------------------------+ | + Model() | | + getScore(): int | | + addBubble(int w, int h): void | | + moveAll(int dx, int dy): void | | + clearInvisibles(int w, int h): void | | + deleteBubblesAtPoint(int x, int y): void | | + drawAll(Graphics g): void | | + testModel(): void | +----------------------------------------------+ When a new model object is created, the score must be zero and the arraylist must be empty. The getScore method returns as result the current score for the game. The addBubble method adds a new bubble to the arraylist of bubbles. The position of the center of the new bubble is random but must be inside a window of width w and height h (the arguments of the addBubble method), like this: new Bubble((int)(w * Math.random()), (int)(h * Math.random())) The moveAll method moves the positions of all the bubbles in the arraylist of bubbles by the amount dx in the x direction and by the amount dy in the y direction. The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1.The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bub
05-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值