[jzoj]7.13自主学习报告

目录

1.Tarjan

                    1.亲戚

2.Network of Schools

2.spfa

Travel

3.并查集

 

1.亲戚

2.食物链


不知道是什么原因,今天突然没比赛了,好不习惯。还好,老师讲了一下Tarjan,spfa以及并查集。

于是乎,我就翻遍了jzoj做了几道题,来给大家分享分享。

1.Tarjan

1.亲戚

题目

若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。

输入:

第一行:三个整数n,m,p,(n<=5000,m<=5000,p<=5000),分别表示有n个人,m个亲戚关系,询问p对亲戚关系。
以下m行:每行两个数Mi,Mj,1<=Mi,Mj<=N,表示Mi和Mj具有亲戚关系。
接下来p行:每行两个数Pi,Pj,询问Pi和Pj是否具有亲戚关系。

输出:

P行,每行一个’Yes’或’No’。表示第i个询问的答案为“具有”或“不具有”亲戚关系。

思路:

这道题不就是并查集的模板题吗?爱动脑的我写完并查集后,突然脑子一转,发现这道题也可以用tarjan缩点啊!
简单来说,tarjan就是把每一块强联通分量缩成一个点,使整个有向图变成一个dag图。
对于这一道题,可以采用tarjan缩点,使整个亲戚关系图中每一个强联通分量缩成一个点(为了方便解释,以下把强联通分量缩成的点简称为“块”),这样一来,属于同一“块”的两个点一定互为亲戚(不难理解),不属于同一“块”的两个点就不是亲戚了。

不同于普通缩点题的是,这道题的亲戚关系图并不是有向图,而是无向图,所以对于每一条边(u,v),要连上u->v和v->u,实现上并不难,我采用链式前向星存图。

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5005;
struct edge
{
    int v,next;
}e[maxn*2];
//链式前向星,由于是无向图,边的数量是m*2
int head[maxn];
int dfn[maxn],low[maxn]; //tarjan的数组
int stack[maxn],ind;    //用栈存储点
bool visit[maxn];//visit[i]表示第i个点是否在栈中
int color[maxn];    //缩点后每一个点属于的“块”
int n,m,p,cnt,tot,num;
void Init()
{            //初始化
    memset(head,-1,sizeof(head));
    for(int i=1;i<=m;i++)
        e[i].v=e[i].next=0;
    cnt=0;
}
void Add(int u,int v)
{  //加边
    e[++cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void Tarjan(int x)
{
         //tarjan缩点模板
    dfn[x]=low[x]=++tot;
    stack[++ind]=x;
    visit[x]=true;
    for(int i=head[x];i!=-1;i=e[i].next)
	{
        int v=e[i].v;
		if(!dfn[v])
		{
            Tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(visit[v])
		{
            low[x]=min(low[x],dfn[v]);
        }
    }
    if(dfn[x]==low[x])
	{     //如果这个点是根节点
        num++;
        while(stack[ind+1]!=x)
		{
            int k=stack[ind--];
            color[k]=num;
        }   //将属于这个强联通分量的点染成同一种颜色
    }
}
int main()
{
    Init();
    scanf("%d %d %d",&n,&m,&p);
    for(int i=1;i<=m;i++)
	{
        int a,b;
        scanf("%d %d",&a,&b);
        Add(a,b);       //由于是无向图,所以要加两条边
        Add(b,a);
    }
    for(int i=1;i<=n;i++)   //对每一个点进行搜索
        if(!dfn[i])
            Tarjan(i);
    for(int i=1;i<=p;i++)
	{
        int a,b;
        scanf("%d %d",&a,&b);
        if(color[a]==color[b])  //属于同一个“块”
            printf("Yes\n");
        else
			printf("No\n");
    }
    return 0;
}

2.Network of Schools

题目

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school. 

输入:

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

输出:

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

思路&题意:

题意:

一个包含1-n号学校的网络,每个学校有个软件分发列表,当学校拿到软件时会把软件分发给列表里的学校。 
问1:一个新软件出现时初始化情况至少需要给多少个学校才能让它到达整个网络? 
问2:至少需要添加多少个名单才能使从任意一个学校开始分发都能充满整个网络? 

转成图的理解就是↓

—给定一个有向图,求:
1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点

2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点— 顶点数<= 100 

思路:

1. 求出所有强连通分量 
2. 每个强连通分量缩成一点,则形成一个有向无环图DAG。 
3. DAG上面有多少个入度为0的顶点,问题1的答案就是多少 
在DAG上要加几条边,才能使得DAG变成强连通的,问题2的答案就是多少 
加边的方法: 
要为每个入度为0的点添加入边,为每个出度为0的点添加出边 
假定有 n 个入度为0的点,m个出度为0的点,如何加边? 
把所有入度为0的点编号 0,1,2,3,4 ….N -1 
每次为一个编号为i的入度0点可达的出度0点,添加一条出边,连到编号为(i+1)%N 的那个出度0点, 这需要加n条边 。
若 m <= n,则加了这n条边后,已经没有入度0点,则问题解决,一共加了n条边 
若 m > n,则还有m-n个入度0点,则从这些点以外任取一点,和这些点都连上边,即可,这还需加m-n条边。 
所以,max(m,n)就是第二个问题的解 
此外:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0;

Code:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define clr( a , x ) memset ( a , x , sizeof (a) );
#define RE freopen("1.in","r",stdin);
#define WE freopen("1.out","w",stdout);
#define SpeedUp std::cout.sync_with_stdio(false);
const int maxn = 105;
const int maxm = maxn * (maxn - 1) / 2;

int head[maxn];
int eCnt;
struct Edge
{
    int v, next;
} edge[maxm];
void add(int u, int v) {
    edge[eCnt].v = v, edge[eCnt].next = head[u], head[u] = eCnt++;
}

int dfn[maxn], low[maxn];
int vis[maxn], inStack[maxn], _stack[maxn];
int rt[maxn];   //所在的强连通分量的序号
int rtCnt;      //强连通分量数目
int idx;        //遍历顺序编号
int top;        //栈索引
void Tarjan(int u) {
    dfn[u] = low[u] = idx++;
    vis[u] = inStack[u] = 1;
    _stack[top++] = u;
    for (int i = head[u]; ~i; i = edge[i].next) {
        int v = edge[i].v;
        if (!vis[v]) {
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (inStack[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) { //连通分量根
        rtCnt++;
        while (top > 0 && _stack[top] != u) {
            top--;
            int v = _stack[top];
            rt[v] = rtCnt;  //赋值为u的话就成并查集了
            inStack[v] = 0;
        }
    }
}

int ind[maxn], outd[maxn];  //强连通分量的出入度
int main() {
    int n, to;
    // RE
    while (cin >> n) {
        top = idx = 1;
        rtCnt = 0; 
        clr(_stack, 0);
        clr(inStack, 0);
        clr(vis, 0);
        clr(head, -1);
        // clr(dfn,0);clr(low,0);clr(rt,0); //没必要

        for (int i = 1; i <= n; ++i) {
            cin >> to;
            while (to) {
                add(i, to);
                cin >> to;
            }
        }
        for (int i = 1; i <= n; ++i) {
            if (!vis[i]) {
                Tarjan(i);
            }
        }

        //遍历每个点的每条边,如果两顶点不是一个分量里的则统计出入度
        //若结构体edge也存起点的话可以直接遍历边数
        clr(ind, 0);
        clr(outd, 0);
        for (int u = 1; u <= n; ++u) {
            for (int i = head[u]; ~i; i = edge[i].next) {
                if (rt[edge[i].v] != rt[u]) {
                    ind[rt[edge[i].v]]++;
                    outd[rt[u]]++;
                }
            }
        }

        //ans1 = 入度为0的强联通分量,ans2 = max(入度0分量数,出度0分量数)
        int cntIn = 0, cntOut = 0;
        for (int i = 1; i <= rtCnt; ++i) {
            if (!ind[i])
                cntIn++;
            if (!outd[i])
                cntOut++;
        }
        cout << cntIn << endl;
        if (rtCnt == 1)     //一个分量时不需要补充边
            cout << "0" << endl;
        else
            cout << max(cntIn, cntOut) << endl;
    }
    return 0;
}

2.spfa

Travel

题目

小Q非常喜欢在自己的国家旅行。小Q所在的国家有N座城市,分别编号为1~n,小Q所在的城市编号为1。小Q现在想知道从他所在的城市出发,到其他N-1个城市的最短路程分别是多少?

输入:

第一行两个整数N,M(1<=n<=1000,1<=M<=100000),分别表示小Q所在的国家有N座城市以及城市间有M条单向道路。
接下来M行,每行三个整数x,y,len(1<=x,y<=n,1<=len<=100000)表示从城市x去到城市y需要走len这么多路程。
输入可能存在重边

输出:

一共N-1行,每行一个整数,第i个整数表示小Q从城市1到城市(i+1)的最短路程。如果不能到达输出-1。

思路:

明显的最短路,SPFA秒掉。注意是单向。

Code:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define maxint 2139062143
using namespace std;
int a[1010][1010],dist[100010],n;
void spfa(int s)
{
	int q[100010],v[100010],h = 0,t = 0,x,i;
	memset(q,0,sizeof(q));
	memset(v,0,sizeof(v));
	memset(dist,127,sizeof(dist));
	dist[s] = 0;
	q[t++] = s;
	v[s] = 1;
	while(h != t)
	{
        x = q[h++];
		v[x] = 0;
		for(i = 1;i <= n;i++)
		{
			if(dist[i] - a[x][i] > dist[x])
			{
				dist[i] = dist[x] + a[x][i];
				if(!v[i])
				{
					q[t++] = i;
					v[i] = 1;
				}
			}
		}
	}
}
int main()
{
	int m;
	scanf("%d%d",&n,&m);
	memset(a,127,sizeof(a));
	for(register int i = 1;i <= m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		a[x][y] = min(a[x][y],z);
	}
	spfa(1);
	for(register int i = 2;i <= n;i++)
		printf("%d\n",dist[i] == 0x3f3f3f3f ? -1 : dist[i]);
	return 0;
}

3.并查集

1.亲戚

题目

若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚。如果x,y是亲戚,那么x的亲戚都是y的亲戚,y的亲戚也都是x的亲戚。

输入:

第一行:三个整数n,m,p,(n<=5000,m<=5000,p<=5000),分别表示有n个人,m个亲戚关系,询问p对亲戚关系。
以下m行:每行两个数Mi,Mj,1<=Mi,Mj<=N,表示Mi和Mj具有亲戚关系。
接下来p行:每行两个数Pi,Pj,询问Pi和Pj是否具有亲戚关系。

输出:

P行,每行一个’Yes’或’No’。表示第i个询问的答案为“具有”或“不具有”亲戚关系。

思路:

这道题目就是一个并查集稍加修饰的一个板子题。从题目描述中我们可以知道,若两个人是亲戚关系,那么他们一定在同一个集合里面。那么就ok了,对于两个亲戚关系的人我们进行 并 的操作,然后对于两个判断的人就找他们的根结点就ok了。是不是就,很简单了?顷刻之间就,做出来了。

ps:是不是觉得这道题很眼熟?想必你们也知道了:我做了两种解法!

Code:

#include<bits/stdc++.h>
using namespace std;
int n,m,p,father[5200];
//n,m,p的解释见题,father数组用来记录该结点属于哪个集合(存储根结点/父亲结点)
struct node
{
    int x,y;
}a[5200];
//a数组用来存储每对亲戚关系。
int getfather(int k)
{
    if(father[k]==k)return k;
    //如果一个点的父亲是自己,那么它就是根结点
    father[k]=getfather(father[k]);
    //这里是记忆化,压缩路径。
    return father[k];
}
//getfather就是找到该结点的根结点,并记忆
void bing(int k,int l)
{
    int fx,fy;
    fx=getfather(k);
    fy=getfather(l);
    father[fx]=fy;
}
//并操作。对两个点的根结点进行如下操作:一个根结点变成另外一个点的儿子结点。那么两个集合就并为一个集合了。
bool cha(int k,int l)
{
    int fx,fy;
    fx=getfather(k);
    fy=getfather(l);
    return fx==fy;
}
//查操作。求出两个结点的根结点,如果相同,那么他们属于一个集合。
void read()
{
    scanf("%d%d%d",&n,&m,&p);
    int x,y;
    for(int i=1;i<=n;i++)
    father[i]=i;
    //每个结点的根结点默认是自己(这就可以理解为有n个集合,每个点是一个集合)
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        bing(x,y);
        //读入亲戚关系后,并他。
    }
}
//读入
void work()
{
    int x,y;
    for(int i=1;i<=p;i++)
    {
        scanf("%d%d",&x,&y);
        if(cha(x,y))printf("Yes\n");
        else printf("No\n");
        //读入后查询
    }
}
int main()
{
    read();//读入
    work();//工作
    return 0;
}

 

2.食物链

题目

动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。
现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。
有人用两种说法对这N个动物所构成的食物链关系进行描述:
第一种说法是“1 X Y”,表示X和Y是同类。
第二种说法是“2 X Y”,表示X吃Y。
此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1<=N<=50,000)和K句话(0<=K<=100,000),输出假话的总数。

输入:

第一行是两个整数N和K,以一个空格分隔。
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。

输出:

只有一个整数,表示假话的数目。

思路:

1.p[x]表示x根结点。r[x]表示p[x]与x关系。r[x]=0 表示p[x]与x同类;1表示p[x]吃x;2表示x吃p[x]。
2.怎样划分一个集合呢?

注意,这里不是根据x与p[x]是否是同类来划分。而是根据“x与p[x]能否确定两者之间关系”来划分,若能确定x与p[x]关系,则它们同属一个集合
3.怎样判断一句话是不是假话?
假设已读入D ,X ,Y ,先利用find()函数得到X,Y所在集合代表元素fx,fy,若它们在同一集合(即fx==fy)则可以判断这句话真伪:
若 D==1 而 r[X]!=r[Y] 则此话为假.(D==1 表示X与Y为同类,而从r[X]!=r[Y]可以推出 X 与 Y 不同类.矛盾.)
若 D==2 而 r[X]==r[Y](X与Y为同类)或者r[X]==(r[Y]+1)%3(Y吃X)则此话为假。
4.上个问题中r[X]==(r[Y]+1)%3这个式子怎样推来?
假设有Y吃X,那么r[X]和r[Y]值是怎样?
我们来列举一下:

r[X]=0&&r[Y]=2 

r[X]=1&&r[Y]=0

r[X]=2&&r[Y]=1

稍微观察一下就知道r[X]=(r[Y]+1)%3;
事实上,对于上个问题有更一般判断方法:
若(r[Y]-r[X]+3)%3!=D-1 ,则此话为假.
5.其他注意事项:
在unionn(d,x,y)过程中若将S(fy)合并到S(fx)上,则相应r[fy]必须更新为fy相对于fx关系。怎样得到更新关系式?
r[fy]=(r[x]-r[y]+d+3)%3;
代码实现如下

Code:

#include <cstdio>
using namespace std;
int p[50001],r[50001],n,k,ans,x,y,d;
int find(int x)
{
    if (x != p[x])
    {
        int fx = find(p[x]);
        r[x] = (r[x] + r[p[x]]) % 3;
        p[x] = fx;
    }
    return p[x];
}
bool unionn(int d,int x,int y)
{
    int fx = find(x),fy = find(y);
    if (fx == fy)
        if ((r[y] - r[x] + 3) % 3 != d) 
			return 1;
        else 
			return 0;
    p[fy] = fx;
    r[fy] = (r[x] - r[y] + d + 3) % 3;
    return 0;
}
int main()
{
    scanf("%d %d",&n,&k);
    for (int i = 1;i <= n;i++) 
		p[i] = i,r[i] = 0;
    for (int i = 1;i <= k;i++)
    {
        scanf("%d %d %d",&d,&x,&y);
        if(x > n || y > n || (x == y && d == 2))
		{
			ans++;
			continue;
		}
        if (unionn(d - 1,x,y)) 
			ans++;
    }
    printf("%d",ans);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值