zoukankan      html  css  js  c++  java
  • BZOJ 4551: [Tjoi2016&Heoi2016]树

    4551: [Tjoi2016&Heoi2016]树

    Time Limit: 20 Sec  Memory Limit: 128 MB

    Description

    在2016年,佳媛姐姐刚刚学习了树,非常开心。现在他想解决这样一个问题:给定一颗有根树(根为1),有以下
    两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个
    结点,可以打多次标记。)2. 询问操作:询问某个结点最近的一个打了标记的祖先(这个结点本身也算自己的祖
    先)你能帮帮他吗?

    Input

    输入第一行两个正整数N和Q分别表示节点个数和操作次数接下来N-1行,每行两个正整数u,v(1≤u,v≤n)表示u到v
    有一条有向边接下来Q行,形如“opernum”oper为“C”时表示这是一个标记操作,oper为“Q”时表示这是一个询
    问操作对于每次询问操作,1 ≤ N, Q ≤ 100000。

    Output

    输出一个正整数,表示结果

    Sample Input

    5 5
    1 2
    1 3
    2 4
    2 5
    Q 2
    C 2
    Q 2
    Q 5
    Q 3

    Sample Output

    1
    2
    2
    1
    倒这做,用并查集维护。
    或者dfs序,用线段树维护。
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 #include<vector>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef long double ld;
    11 typedef pair<int,int> pr;
    12 const double pi=acos(-1);
    13 #define rep(i,a,n) for(int i=a;i<=n;i++)
    14 #define per(i,n,a) for(int i=n;i>=a;i--)
    15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
    16 #define clr(a) memset(a,0,sizeof(a))
    17 #define pb push_back
    18 #define mp make_pair
    19 #define fi first
    20 #define sc second
    21 #define pq priority_queue
    22 #define pqb priority_queue <int, vector<int>, less<int> >
    23 #define pqs priority_queue <int, vector<int>, greater<int> >
    24 #define vec vector
    25 ld eps=1e-9;
    26 ll pp=1000000007;
    27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
    28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
    29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
    30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
    31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
    32 ll read(){ ll ans=0; char last=' ',ch=getchar();
    33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
    34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    35 if(last=='-')ans=-ans; return ans;
    36 }
    37 const int N=100005,M=N+N;
    38 int fa[N],tf[N],v[M],Next[M],head[N],ans[N],f[N],e,ansnu,n,m;
    39 struct node{
    40     int f,u;
    41 }Q[N];
    42 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e;}
    43 int getfa(int x){
    44     if (fa[x]==x) return x;
    45     else return fa[x]=getfa(fa[x]);
    46 }
    47 void dfs(int u,int f_){
    48     for (int i=head[u];i;i=Next[i]){
    49         if (v[i]==f_) continue;
    50         dfs(v[i],u);
    51     }
    52     if (!f[u]) fa[u]=f_; tf[u]=f_;
    53 }
    54 int main(){
    55     n=read(),m=read(); int x,y; char ch[5];
    56     for (int i=1;i<n;i++) {
    57         x=read(),y=read();
    58         add(x,y); add(y,x);
    59     }
    60     for (int i=1;i<=n;i++) fa[i]=i; f[1]=1;
    61     for (int i=1;i<=m;i++) {
    62         scanf("%s",ch);
    63         if (ch[0]=='C'){
    64             Q[i].f=0; Q[i].u=read();
    65             f[Q[i].u]++;
    66         } else Q[i].f=1,Q[i].u=read();
    67     }
    68     dfs(1,0);
    69     for (int i=m;i>=1;i--){
    70         if (Q[i].f) ans[++ansnu]=getfa(Q[i].u);
    71         else { 
    72             f[Q[i].u]--;
    73             if (!f[Q[i].u]) fa[Q[i].u]=tf[Q[i].u];
    74         }
    75     }
    76     for (int i=ansnu;i>=1;i--){
    77         printf("%d
    ",ans[i]);
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    死工资的缓慢呆板已不能适合这个新型的商业社会。
    java获取前一天时间SimpleDateFormat,java判断某个时间段
    设置 DNS,防止 DNS 污染,清除 DNS 缓存ipconfig /flushdns
    win7 怎么设置开机直接进入桌面? netplwiz 命令
    AWS免费云服务套餐申请步骤及常见问题
    阿里云自定义镜像可以免费保存,ECS实例到期后自定义镜像手动快照不会被删除
    php获取字符串长度函数strlen和mb_strlen
    PHP HTML混写,PHP中把大块HTML文本直接赋值给字符串变量的方法
    人工智能跟脑神经科学没有关系。没有出差错的机会,就没有进化的可能。要想自己把事情做成功,就需要弄清楚事物的本质。
    python GIL 全局锁,多核cpu下的多线程性能究竟如何?
  • 原文地址:https://www.cnblogs.com/SXia/p/7718081.html
Copyright © 2011-2022 走看看