钱柜娱乐手机客户端:离线并查集求连通块个数,线段树区间合并

钱柜娱乐手机客户端:离线并查集求连通块个数,线段树区间合并

 

主题材料链接:

标题大要:n个点m条边(边1,边2…边m卡塔 尔(阿拉伯语:قطر‎,q个要摧毁的边,求按梯次每摧毁一条边后图中连通块的个数

 

主题素材解析:离线并查集,反向加边,先将q条路全体摧毁后的连接块个数求出来,然后加边就可以,每加一条边,若两点不在同三翻五次通块中,则统风流倜傥且连通块个数减1

 

 

#include 
#include 
int const MAX = 1e5 + 5;
int fa[MAX], n, m;
int x[MAX], y[MAX], r[MAX];
int ans, res[MAX];
bool vis[MAX];

void UF_set()
{
 for(int i = 0; i <= n; i++)
  fa[i] = i;
}

int Find(int x)
{
 return x == fa[x] ? x : fa[x] = Find(fa[x]);
}

void Union(int a, int b)
{
 int r1 = Find(a);
 int r2 = Find(b);
 if(r1 != r2)
 {
  fa[r2] = r1;
  ans --;
 }
}

int main()
{
 while(scanf("%d %d", &n, &m) != EOF)
 {
  UF_set();
  ans = n;
  memset(vis, false, sizeof(vis));
  for(int i = 1; i <= m; i++)
   scanf("%d %d", &x[i], &y[i]);
  int q;
  scanf("%d", &q);
  for(int i = 1; i <= q; i++)
  {
   scanf("%d", &r[i]);
   vis[r[i]] = true;
  }
  for(int i = 1; i <= m; i++)
   if(!vis[i])
    Union(x[i], y[i]);
  for(int i = q; i >= 1; i--)
  {
   res[i] = ans;
   Union(x[r[i]], y[r[i]]); 
  }
  for(int i = 1; i < q; i++)
   printf("%d ", res[i]);
  printf("%d\n", res[q]);
 }
}

 

1601 War (离线并查集求连通块个数) 1601: War
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 130 Solved: 38
[Submit][Status][Web Board] Description AME decided to
de…

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K
(Java/Others) Total Submission(s): 6065 Accepted Submission(s): 2344

亚洲城ca88会员登录 ,Problem Description During the War of Resistance Against Japan, tunnel
warfare was carried out extensively in the vast areas of north China
Plain. Generally speaking, villages connected by tunnels lay in a line.
Except the two at the ends, every village was directly connected with
two neighboring ones.

Frequently the invaders launched attack on some of the villages and
destroyed the parts of tunnels in them. The Eighth Route Army commanders
requested the latest connection state of the tunnels and villages. If
some villages are severely isolated, restoration of connection must be
done immediately!

Input The first line of the input contains two positive integers n and m
(n, m ≤ 50,000) indicating the number of villages and events. Each of
the next m lines describes an event.

There are three different events described in different format shown
below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th
village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.

Output Output the answer to each of the Army commanders’ request in
order on a separate line.

Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Source POJ Monthly
钱柜娱乐手机客户端:离线并查集求连通块个数,线段树区间合并。Recommend LL | We have carefully selected several similar problems for
you: 1542 1255 1698 1828 2871
主题素材概略:输入n,m:n表示有n个城市不断,m表示上面有m行指令。D
X代表破坏X那一个城墙,奇骏表示修复近日一遍破坏的都会,Q
X代表查询以X点为着力的意气风发体化三回九转中最大的接连个数并出口。
依照题意画出下图,方便清楚:
钱柜娱乐手机客户端 1
解题思路:首先是建树,在构造体里定义ls记录该间距左端点的最洛桑续个数,rs记录区间右端点起来的最都林续个数,ms表示该间距最大的连天个数。
代码中的注释解释的相比较清楚。
详细代码。

#include 
#include 
#include 

using namespace std;

struct node
{
    int l,r;
    int ls,rs,ms;//分别表示左边最大连续,右边最大连续,以及整个区间内的最大连续长度
} s[50050*3];

int n,m;
int op[50010];

void InitTree(int l,int r,int k)
{
    s[k].l=l;
    s[k].r=r;
    s[k].ls=s[k].rs=s[k].ms=r-l+1;//最开始的时候全都是连着的。所以长度为r-l+1
    if (l==r)
        return ;
    int mid=(l+r)/2;
    InitTree(l,mid,k*2);
    InitTree(mid+1,r,k*2+1);
}

void UpdataTree(int x,int flag,int k)//x表示修复或者破坏的数字,flag用来标记是破坏还是修复
{
    if (s[k].l==s[k].r)
    {
        if (flag==1)
            s[k].ls=s[k].rs=s[k].ms=1;//修复
        else
            s[k].ls=s[k].rs=s[k].ms=0;//破坏
        return ;
    }
    int mid=(s[k].l+s[k].r)/2;
    if (x<=mid)
        UpdataTree(x,flag,2*k);
    else
        UpdataTree(x,flag,2*k+1);
    if(s[2*k].ls == s[2*k].r-s[2*k].l+1)//左区间的左连续=左子树的长度,就说名左区间的数全部连续,(左子树区间满了),整个区间的左区间就应该加上有区间的左部分。
        s[k].ls =s[2*k].ls+s[2*k+1].ls;
    else
        s[k].ls=s[2*k].ls;
    if(s[2*k+1].rs==s[2*k+1].r-s[2*k+1].l+1)//同理
        s[k].rs=s[2*k+1].rs+s[2*k].rs;
    else
        s[k].rs=s[2*k+1].rs;
    s[k].ms=max(max(s[2*k].ms,s[2*k+1].ms),s[2*k].rs+s[2*k+1].ls);//整个区间内的最大连续应为:左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中取最大
}

int SearchTree(int x,int k)
{
    if(s[k].l==s[k].r||s[k].ms==0||s[k].ms==s[k].r-s[k].l+1)//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
        return s[k].ms;
    int mid=(s[k].l+s[k].r)/2;
    if (x<=mid)
    {
        if (x>=s[2*k].r-s[2*k].rs+1)//判断当前这个数是否在左区间的右连续中,其中s[2*k].r-s[2*k].rs+1代表左子树右边连续区间的左边界值,即有连续区间的起点
            return s[2*k].rs+s[2*k+1].ls;//也可以SearchTree(x,2*k)+SearchTree(mid+1,2*k+1);
        else
            return SearchTree(x,2*k);
    }
    else
    {
        if (x<=s[2*k+1].l+s[2*k+1].ls-1)//判断当前这个数是否在左区间的右连续中,其中s[2*k].r-s[2*k].rs+1代表左子树右边连续区间的左边界值,即有连续区间的起点
            return s[2*k].rs+s[2*k+1].ls;//这种方法SearchTree(x,2*k+1)+SearchTree(mid,2*k);也是可以的,但是比较浪费时间
        else
            return SearchTree(x,2*k+1);
    }
}

int main()
{
    int x;
    char ch[2];
    while (~scanf ("%d%d",&n,&m))
    {
        int top=0;
        InitTree(1,n,1);
        while (m--)
        {
            scanf("%s",ch);
            if (ch[0]=='D')
            {
                scanf("%d",&x);
                op[top++]=x;
                UpdataTree(x,0,1);
            }
            else if (ch[0]=='Q')
            {
                scanf("%d",&x);
                printf ("%d\n",SearchTree(x,1));
            }
            else
            {
                if (x>0)
                {
                    x=op[--top];
                    UpdataTree(x,1,1);
                }
            }
        }
    }
    return 0;
}

 

1540 Tunnel Warfare (线段树区间合并)标题链接: Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit…

poj2892 Tunnel Warface

 

Tunnel Warfare

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7434   Accepted: 3070

 

Description

During the War of Resistance Against Japan, tunnel warfare was carried
out extensively in the vast areas of north China Plain. Generally
speaking, villages connected by tunnels lay in a line. Except the two at
the ends, every village was directly connected with two neighboring
ones.

Frequently the invaders launched attack on some of the villages and
destroyed the parts of tunnels in them. The Eighth Route Army commanders
requested the latest connection state of the tunnels and villages. If
some villages are severely isolated, restoration of connection must be
done immediately!

Input

The first line of the input contains two positive integers n and m
(n, m ≤ 50,000) indicating the number of villages and events. Each
of the next m lines describes an event.

There are three different events described in different format shown
below:

D x: The x-th village was destroyed.Q x: The Army commands
requested the number of villages that x-th village was directly or
indirectly connected with including itself.R: The village destroyed
last was rebuilt.

 

Output

Output the answer to each of the Army commanders’ request in order on a
separate line.

Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1
0
2
4

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

Source

POJ Monthly–2006.07.30, updog

 

 

 

平衡树的应用

平衡树中保留全部被炸掉的节点。

对于炸毁和修补操作,直接在平衡树中插入或删除。

对于查询操作,先推断该节点是或不是被炸掉,要是被炸毁答案为0,不然在平衡树中求出前驱x和后继y,答案为y-x-1。

还应该有风度翩翩种方法是二分+树状数组,认为速度比那些慢就从未有过写…

 

 

 

#include
#include
#include
#include
#include
#include
#include
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 50005
#define pa pair
#define INF 1000000000
using namespace std;
int n,m,x,tot=0,rt=0,l[MAXN],r[MAXN],rnd[MAXN],v[MAXN];
char op;
bool f[MAXN];
stack st;
inline int read()
{
 int ret=0,flag=1;char ch=getchar();
 while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
 while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
 return ret*flag;
}
inline void rturn(int &k)
{
 int tmp=l[k];
 l[k]=r[tmp];r[tmp]=k;
 k=tmp;
}
inline void lturn(int &k)
{
 int tmp=r[k];
 r[k]=l[tmp];l[tmp]=k;
 k=tmp;
}
inline void ins(int &k,int x)
{
 if (!k){k=++tot;v[k]=x;l[k]=r[k]=0;rnd[k]=rand();return;}
 if (x=v[k]) return suc(r[k],x);
 else {int tmp=suc(l[k],x);return tmp==n+1?v[k]:tmp;}
}
inline int getans(int x)
{
 if (f[x]) return 0;
 return suc(rt,x)-pre(rt,x)-1;
}
int main()
{
 memset(f,false,sizeof(f));
 n=read();m=read();
 while (m--)
 {
  op=getchar();while (op<'A'||op>'Z') op=getchar();
  if (op=='D') {x=read();f[x]=true;st.push(x);ins(rt,x);}
  else if (op=='Q') {x=read();printf("%d\n",getans(x));}
  else {del(rt,st.top());f[st.top()]=false;st.pop();}
 }
}

 

Tunnel Warface Tunnel Warfare Time Limit:
1000MS Memory Limit: 131072K Total Submissions: 7434 Accepted: 3070
Description During the War of Resistance Against Japan, tunnel…

Sample Input

3 1
1 2
1
1
4 4
1 2
2 3
1 3
3 4
3
2 4 3

hdu 1540 Tunnel Warfare (线段树区间归并卡塔 尔(英语:State of Qatar)

标题链接:

 

Input

The first line contains two integers N and M — the number of villages
and roads, (2 ≤ N ≤ 100000; 1 ≤ M ≤ 100000). Each of the next M lines
contains two different integers u, v (1<=u, v<=N)—which means
there is a road between u and v. The next line contains an integer Q
which denotes the quantity of roads AME wants to destroy (1 ≤ Q ≤ M).
The last line contains a series of numbers each of which denoting a road
as its order of appearance — different integers separated by spaces.

admin

网站地图xml地图