[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年多了, ...
随机推荐
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1
(5)SELECT (5-2) DISTINCT (5-3)TOP(<top_specifications>) (5-1)<select_list> (1)FRO ...
- Huawei HG556a A版 刷 openwrt
一直想玩玩openwrt,调研了一下 HG556a尽管散热很烂,但性价比超高,于是淘宝入手一台A版,A版和C版区别为wifi芯片: 到货后在网上找了几个教程便开始动手刷openwrt,但刷机的过程中还 ...
- 使用泛型 类型“System.Collections.Generic.IEnumerator<T>”需要 1 个类型参数
解决办法:添加 using System.Collections:命名空间
- 【分享】SQL Server优化50法
虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种: 没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) I/ ...
- MySQL 主主同步配置和主从配置步骤
★预备知识 : 1.双机热备 对于双机热备这一概念,我搜索了很多资料,最后,还是按照大多数资料所讲分成广义与狭义两种意义来说. 从广义上讲,就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服 ...
- [转载]Dotfuscator Professional Edition 4.9.7500.9484 混淆工具破解版+使用教程
如有转载,请注明出处: http://www.cnblogs.com/flydoos/archive/2012/01/26/2329536.html Dotfuscator Professional ...
- uva 1377
比较不错的一个题,关键是理解状态转移 #include<algorithm> #include<cstdio> #include<cstring> #include ...
- KafkaSpout: PartitionManager的行为分析
KafkaSpout的核心逻辑都是由PartitionManager来实现的. 但是这个类实现时候需要考虑的东西有些多,0.92至0.93,至当前(2015.3.14)的master一直在变化.在这里 ...
- 盘点 OSX 上最佳的 DevOps 工具
[编者按]对于运维人员来说,他们往往需要各种各样的工具来应对工作需求,近日 Dustin Collins 通过「The Best DevOps Tools on OSX」一文对 OSX 平台上的工具进 ...
- PYTHON设计模式,创建型之简单工厂模式
这个系统,感觉思路清爽,,相信多练练,多思考,就会熟悉的.. http://www.jianshu.com/p/2450b785c329 #!/usr/bin/evn python #coding:u ...