CSU 1811 Tree Intersection(启发式合并)
来源:互联网 发布:纯阳道长捏脸数据 编辑:程序博客网 时间:2024/05/21 22:45
代码:
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MAX 100005#define MAXN 1000005#define maxnode 205#define sigma_size 2#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;//const double pi = acos(-1.0);const double inf = 1e18;const double eps = 1e-9;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/struct Edge{ int v,next,id;}edge[MAX*2];int head[MAX];int ans[MAX];int sum[MAX];int c[MAX];map<int,int> cnt[MAX];int tot;void init(){ mem(head,-1); mem(ans,0); mem(sum,0); tot=0;}void add_edge(int a,int b,int id){ edge[tot]=(Edge){b,head[a],id}; head[a]=tot++;}void dfs(int u,int fa,int id){ cnt[u][c[u]]=1; if(sum[c[u]]>1) ans[id]++; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(v==fa) continue; dfs(v,u,edge[i].id); if(cnt[u].size()<cnt[v].size()){ swap(cnt[u],cnt[v]); ans[id]=ans[edge[i].id]; } for(map<int,int>::iterator it=cnt[v].begin();it!=cnt[v].end();it++){ int x=it->first,y=it->second; if(cnt[u].count(x)){ cnt[u][x]+=y; if(cnt[u][x]==sum[x]) ans[id]--; } else{ cnt[u][x]=y; if(cnt[u][x]<sum[x]) ans[id]++; } } }}int main(){ //freopen("in.txt","r",stdin); int n; while(cin>>n){ init(); for(int i=1;i<=n;i++){ scanf("%d",&c[i]); cnt[i].clear(); sum[c[i]]++; } //cout<<all<<endl; for(int i=1;i<n;i++){ int a,b; scanf("%d%d",&a,&b); add_edge(a,b,i); add_edge(b,a,i); } dfs(1,-1,0); for(int i=1;i<n;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- CSU 1811 Tree Intersection(启发式合并)
- CSU 1811 Tree Intersection(启发式合并)
- CSU 1811 Tree Intersection 平衡树启发式合并
- CSU 1811 Tree Intersection(线段树+启发式合并 解法)
- CSU 1811 Tree Intersection(Treap启发式合并)
- [CSU - 1811 (湖南省赛16)] Tree Intersection (启发式合并)
- CSU 1811 Tree Intersection
- CSU 1811 Tree Intersection
- CSU 1811 Tree Intersection(莫队算法)
- CSU 1811 Tree Intersection(dfs序+莫队算法)
- csu 1811 Tree Intersection 2016湖南省赛 I
- BZOJ 2212([Poi2011]Tree Rotations-启发式合并)
- hdu4812 D Tree,平衡树,启发式合并
- poj1741 Tree,平衡树,启发式合并
- 【平衡树启发式合并】POJ1741[Tree]题解
- 启发式合并/dsu on tree 姿势
- CSU 1811 Tree Intersection 处理树问题的一个重要思路!!!
- 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
- C++ 默认参数
- 图算法(1) - 基础知识
- Lightoj1202——Bishops(找规律)
- Unity 优化
- 正则表达式
- CSU 1811 Tree Intersection(启发式合并)
- Android Studio:正确导入SO相关文件
- 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
- 基于深度学习的人脸识别系统系列(Caffe+OpenCV+Dlib)——【一】如何在Visual Studio中像使用OpenCV一样使用Caffe
- Unity3D Mesh小课堂(二)为三角形添加纹理
- Java基础
- Java中Final关键字的使用
- CURL
- Java NIO详解