#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. Web开发——Http协议剖析

    Http,即超文本传输协议,是建立在TCP/IP协议的基础上.在Web开发的过程中,Http协议是十分重要的,浏览器与服务器之间的交互就是基于Http协议的.Http协议如果展开全面讲解会有很多内容, ...

  2. TDBAdvGrid 只读状态下复制功能

    DataSource1.AutoEdit := false;

  3. delphi中的临界区

    var fLock:TRTLCriticalSection; //定义临界区域 // 初始化 InitializeCriticalSection(fLock); //进入临界区 EnterCritic ...

  4. 使用urllib2的HttpResponse导致内存不回收(内存泄漏)

    问题出现环境:python 2.7.1(X)及以下, Windows(或CentOS) 这个问题产生在lib/urllib2.py的line 1174 (python 2.7.1),导致形成了cycl ...

  5. H264相关代码

    H.264格式的视频打包成RTP后进行发送,编译环境为VC6++ #include <stdio.h> #include <stdlib.h> #include <str ...

  6. 【BZOJ 2038】[2009国家集训队]小Z的袜子(hose)

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...

  7. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...

  8. 关于sqlmap无法打开的问题解决办法

    sqlmap无法打开这个问题困扰了我几天,今天才从一个论坛的视频教程里看到这个办法,有可能对其他有些情况依旧没用,但是希望和我一样状况的人看到这篇文章,能收到一些启发 我之前百度了很久,google了 ...

  9. 【HTTP】Fiddler(一) - Fiddler简介和使用

    1.为什么是Fiddler? 抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: a.Firebug虽然可以抓包 ...

  10. redis 性能监控和排查

    最近项目中接连遇到redis出现瓶颈的问题,现在把排查的一些经验记录下来备查,本篇只是思路的整理,不涉及具体的使用. 大体的思路如下: 1.通过slow log查看 参考 http://www.cnb ...