#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. CR0,CR3寄存器

    驱动在hook系统函数的时候通常要将只读属性暂时的屏蔽掉,主要有三种方法 1.修改CR0寄存器的WP位,使只读属性失效(这是网上用的最多的方法),切忌使用完之后立马修改回来 2.只读的虚拟地址,通过C ...

  2. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  3. Ducci Sequence

    Description   A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers ( ...

  4. js截取所需字符串长度

    //title :字符串  :interceptLength:所需的长度 function TitleThumbnail(title, interceptLength, thumbnailCharac ...

  5. oracle 求两个时间点直接的分钟、小时数

    select )) h, )) m, )) s from gat_data_record gdr where gdr.enddt between to_date('2011-1-1','yyyy-mm ...

  6. storm sum aggregate 原语 聚合 本地测试

    编写storm程序,对数据进行聚合并且写入到mysql, 本文  主要说明数据中有多个字段需要进行sum或其他操作时的程序写法 1.主程序main方法,storm 拓扑运行入口 public clas ...

  7. 严重: The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it.

    今日在重新部署项目时出现此问题,虽然对项目无影响,但问题就是问题.完整信息如下(使用idea工具): 十二月 05, 2015 11:44:27 上午 org.apache.catalina.star ...

  8. Javascript字典操作

    <script type="text/javascript">        var dic = new Array();      //注意它的类型是Array    ...

  9. 移动web开发入门级

    http://www.infoq.com/cn/articles/development-of-the-mobile-web-deep-concept/

  10. C++ 嵌套类使用(二)

    C++嵌套类 1.   嵌套类的名字只在外围类可见. 2.   类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员.嵌套类可以访问外围类的成员(通过对象.指针或者引用). 3 ...