[2015编程之美] 第一场A
#1156 : 彩色的树
描述
给定一棵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的更多相关文章
- [2015编程之美] 第一场C
题目3 : 质数相关 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 两个数a和 b (a<b)被称为质数相关,是指a × p = b,这里p是一个质数.一个集合S被 ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- 2015编程之美 初赛第一场C题 质数相关 二分图的最大匹配
质数相关 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/msbop2015round2a/prob ...
- 2015 编程之美初赛第一场 AC题
题目1 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, 2, …, n.树中有n - 1条边,任意两个节点间恰好有一条路 ...
- 2015 多校赛 第一场 1007 (hdu 5294)
总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...
- 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 ...
- [2015编程之美] 资格赛C
#1150 : 基站选址 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 需要在一个N × M的网格中建立一个通讯基站,通讯基站仅必须建立在格点上. 网格中有A个用户,每个 ...
- hdu5294||2015多校联合第一场1007 最短路+最大流
http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...
- 2015年北京的第一场雪-关于android学习的思考(84)
今天是2015年11月6日,今天北京下了大雪,我听着民谣,发现丢火车的摇滚也还不错,我身体的一部分毛发也发生了变异,由黑色变成红色,一切来的太突然了......不知不觉学习android开发2年多了, ...
随机推荐
- XE5 ANDROID平台 调用 webservice
服务端需要midas.dll XE5对android的平台支持很有吸引力,虽然目前用来直接开发应用到安卓市场卖赚钱可能性估计不大(安卓市场目前国内好像都是免费的天下),但是对于企业应用很是很有帮助 ...
- (转载)总结一下SQL语句中引号(')、quotedstr()、('')、format()在SQL语句中的用法
总结一下SQL语句中引号(').quotedstr().('').format()在SQL语句中的用法 总结一下SQL语句中引号(').quotedstr().('').format()在SQL语句中 ...
- 关于python多线程编程中join()和setDaemon()的一点儿探究
关于python多线程编程中join()和setDaemon()的用法,这两天我看网上的资料看得头晕脑涨也没看懂,干脆就做一个实验来看看吧. 首先是编写实验的基础代码,创建一个名为MyThread的 ...
- 【转】C#路径/文件/目录/I/O常见操作汇总
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- drop column与set unused
8i以前,如果需要删除表中的列,需要删除表然后重新建.现在,但我们需要删除一个列时,可以有以下两种方法: Logical Delete Physical Delete Logical Delete(逻 ...
- X86 复制本地 生成有问题、类型初始值设定项引发异常
一. 选择项目,右击属性——生成——目标平台 选择x86就可以了. 二. 有的时候你发现你项目中的dll没有生成到本地bin,这时右击它属性,到它引用的地方复制引用dll放到部署环境中,你会发现一样报 ...
- iOS通过http post上传图片 (转)
转载自:http://www.cocoachina.com/bbs/read.php?tid=89985 由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他 ...
- c++ string char* const char*
#include <iostream> #include <string> #include <cstring> using namespace std; int ...
- [Firefly引擎][学习笔记四][已完结]服务器端与客户端的通讯
原地址:http://www.9miao.com/question-15-54981.html 传送门:学习笔记一学习笔记二学习笔记三 前言:学习笔记三是模块封装,这个在持续开发中会不断更新, 因为写 ...
- python参考手册--第3章类型和对象
1.对象的身份.类型.值 (1)身份:对象在内存中位置的指针,地址值, >>> a = [1,2,3,4,5] >>> id(a)48497328 >> ...