#1156 : 彩色的树

时间限制:2000ms
单点时限:1000ms
内存限制:256MB

描述

给定一棵n个节点的树,节点编号为1, 2, …, n。树中有n - 1条边,任意两个节点间恰好有一条路径。这是一棵彩色的树,每个节点恰好可以染一种颜色。初始时,所有节点的颜色都为0。现在需要实现两种操作:

1. 改变节点x的颜色为y;

2. 询问整棵树被划分成了多少棵颜色相同的子树。即每棵子树内的节点颜色都相同,而相邻子树的颜色不同。

输入

第一行一个整数T,表示数据组数,以下是T组数据。

每组数据第一行是n,表示树的节点个数。接下来n - 1行每行两个数i和j,表示节点i和j间有一条边。接下来是一个数q,表示操作数。之后q行,每行表示以下两种操作之一:

1. 若为"1",则询问划分的子树个数。

2. 若为"2 x y",则将节点x的颜色改为y。

输出

每组数据的第一行为"Case #X:",X为测试数据编号,从1开始。

接下来的每一行,对于每一个询问,输出一个整数,为划分成的子树个数。

数据范围

1 ≤ T ≤ 20

0 ≤ y ≤ 100000

小数据

1 ≤ n, q ≤ 5000

大数据

1 ≤ n, q ≤ 100000

样例输入
2
3
1 2
2 3
3
1
2 2 1
1
5
1 2
2 3
2 4
2 5
4
1
2 2 1
2 3 2
1
样例输出
Case #1:
1
3
Case #2:
1
5 TLE代码,能过小数据。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 100010 struct Edge
{
int to,next;
}edge[N];
int tot;
int col[N];
int head[N];
int n,m;
int ans; void init()
{
tot=;
ans=;
for(int i=;i<=n;i++) col[i]=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void update(int u,int c)
{
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(col[v]!=col[u] && col[v]==c) ans--;
else if(col[v]==col[u] && col[v]!=c) ans++;
}
col[u]=c;
}
int main()
{
int T,iCase=;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
printf("Case #%d:\n",iCase++);
scanf("%d",&m);
while(m--)
{
int op,pos,c;
scanf("%d",&op);
if(op==) printf("%d\n",ans);
else
{
scanf("%d%d",&pos,&c);
update(pos,c);
}
}
}
return ;
}

正解代码:

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
#define ll long long
#define N 100010 struct Edge
{
int to,next;
}edge[N<<]; int n,m;
int ans;
int tot;
int fa[N];
int vis[N];
int col[N];
int head[N];
map<int,int> mp[N]; void init()
{
tot=;
ans=;
for(int i=;i<=n;i++)
{
col[i]=;
mp[i].clear();
}
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u)
{
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(!vis[v])
{
fa[v]=u;
mp[u][]++;
dfs(v);
}
}
}
void update(int x,int c)
{
if(col[x]==c) return;
int y=fa[x];
//对儿子节点
ans+=mp[x][col[x]];
ans-=mp[x][c];
//对父亲节点
if(y!=-)
{
if(c!=col[y]) ans++;
if(col[x]!=col[y]) ans--;
mp[y][col[x]]--;
mp[y][c]++;
}
col[x]=c;
}
int main()
{
int T,iCase=;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
fa[]=-;
dfs();
printf("Case #%d:\n",iCase++);
scanf("%d",&m);
while(m--)
{
int op,pos,c;
scanf("%d",&op);
if(op==) printf("d\n",ans);
else
{
scanf("%d%d",&pos,&c);
update(pos,c);
}
}
}
return ;
}

[2015编程之美] 第一场A的更多相关文章

  1. [2015编程之美] 第一场C

    题目3 : 质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被 ...

  2. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  3. 2015编程之美 初赛第一场C题 质数相关 二分图的最大匹配

    质数相关 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/msbop2015round2a/prob ...

  4. 2015 编程之美初赛第一场 AC题

    题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...

  5. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  6. 2015 多校赛 第一场 1002 (hdu 5289)

    Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...

  7. [2015编程之美] 资格赛C

    #1150 : 基站选址 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 需要在一个N × M的网格中建立一个通讯基站,通讯基站仅必须建立在格点上. 网格中有A个用户,每个 ...

  8. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  9. 2015年北京的第一场雪-关于android学习的思考(84)

    今天是2015年11月6日,今天北京下了大雪,我听着民谣,发现丢火车的摇滚也还不错,我身体的一部分毛发也发生了变异,由黑色变成红色,一切来的太突然了......不知不觉学习android开发2年多了, ...

随机推荐

  1. linux 输入子系统(2)----简单实例分析系统结构(input_dev层)

    实例代码如下: #include <linux/input.h> #include <linux/module.h> #include <linux/init.h> ...

  2. PyQt4学习记录之事件和信号

    事件是任何 GUI程序中很重要的部分.所有 Python GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网 ...

  3. jquery.prompt.js 弹窗的使用

    /*** * Prompt提示语插件 * 编写时间:2013年4月8号 * version:Prompt.1.0.js * author:小宇<i@windyland.com> ***/ ...

  4. Google code: Why ‘Everything up-to-date’ when pushing (git)

    原文链接:http://blog.rexzhao.com/2011/11/28/google-code-git-everything-up-to-date-when-push.html 第一次在Goo ...

  5. shell自定义函数

    Linux中提供了很多内置的函数,但有时我们需要根据自己的需求来创建自定义函数.下面介绍一下关于shell编程中的自定义函数. 1.函数定义 function hello(){    echo &qu ...

  6. linux驱动系列之arm汇编

    在arm平台学习linux时,会遇到arm汇编指令,arm汇编指令与8086汇编指令很多地方都不同,在此记下来以免后面忘了,同时在学习了汇编指令之后分析一些汇编指令编写的代码. 一.相对跳转指令b.b ...

  7. GitHub 有哪些优秀的项目

    GitHub 有哪些优秀的项目 http://www.zhihu.com/question/20584141

  8. 团体程序设计天梯赛-练习集L1-008. 求整数段和

    L1-008. 求整数段和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 杨起帆 给定两个整数A和B,输出从A到B的所有整数以及这些 ...

  9. hdu 4726

    贪心 不是很难  各种细节注意 #include <cstdio> #include <cstring> #include <algorithm> using na ...

  10. jquery类选择器无法取得对象问题原因

    <html> <script type="text/javascript" src="jquery-1.9.1.js"></scr ...