Information Transmission-2(2019第十二届ICPC-ACM河南省赛-G-强连通分量)

Information Transmission-2(2019第十二届ICPC-ACM河南省赛-G-强连通分量)

看到这题第一感觉是惊喜,因为作为新生遇到自己学过的算法题不容易。但是等我想上手敲的时候却无从下手!!不知不觉连不久前刚刷过的专题都手生了。。。

学的东西多了,复习就变成了一件大事……

题目描述

Information plays a very important role in people’s social life. For example, scientific research must not only obtain the results of others 'research in time, but also publish and inform others of the results of their own research in time. Only through this exchange of information can we continue to develop. Network communication is based on computer communication network for information transmission, exchange and utilization.

There are n base points and there is a certain transfer relationship between them., the information can be transmitted from point A to point B, and point B to point C, and so on. Now there is a massage to be broadcasted , at least which base points to be sent the message first, so that all the base points can be received?

输入

The first line of the input contains one integer T T T, which is the number of test cases ( 1 &lt; = T &lt; = 8 ) (1&lt;=T&lt;=8) (1<=T<=8). Each test case specify:
* Line 1: n ( 2 ≤ n ≤ 100 ) n (2 ≤ n ≤ 100) n(2n100)
*Line 2~n+1: each line contains a 01 string of length n .
ie:if the sixth column in the third row is 1 1 1, it means that the third base point can pass the message to the sixth base point.

输出

For each test case generate a single line: minimum number of base that the massage must be sent first, so that all the basis points can be received.

样例输入

2
3
010
001
000
6
010000
001001
000100
010000
001000
000000

样例输出

1
2

题意

有一个好消息要传给 n n n个人,但这些人不是彼此都有通信的,只有一部分人能传话给别人(有向图),告诉你都有谁可以传给谁,问你最少要通知几个人能使大家都知道这个消息(强连通)。

输入格式是加入 a [ i ] [ j ] a[i][j] a[i][j]是1,那么 i i i 就可以传话给 j j j
否则就不能。

代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <stack>
#include <string.h>
#define maxn 10010
#define inf 0x3f3f3f3f
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
struct edge {
	int v, next, u, id;
}G[2 * maxn];
int head[maxn], cnt;
int n, m;
int a[maxn];
int scccnt, sccno[maxn];
int dfn[maxn], low[maxn];
int tclock;
stack<int> q;
int ans, num;
int indegree[maxn];
void init() {
	memset(head, -1, sizeof(head));
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(sccno, 0, sizeof(sccno));
	scccnt = tclock = 0;
	memset(indegree, 0, sizeof(indegree));
	cnt = 0;
}
void add_edge(int u, int v) {
	G[cnt].u = u;
	G[cnt].v = v;
	G[cnt].next = head[u];
	G[cnt].id = cnt;
	head[u] = cnt++;
}
void read() {
	scanf("%d", &n);
	_rep(i, 1, n) {
		_rep(j, 1, n) {
			int x;
			scanf("%1d", &x);
			if (x) {
				add_edge(i, j);
			}
		}
	}
}
void tarjin(int u) {
	dfn[u] = low[u] = ++tclock;
	q.push(u);
	for (int i = head[u]; i != -1; i = G[i].next) {
		int v = G[i].v;
		if (!dfn[v]) {
			tarjin(v);
			low[u] = min(low[u], low[v]);
		}
		else if (!sccno[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if (dfn[u] == low[u]) {
		scccnt++;
		int v = -1;
		while (v != u) {
			v = q.top();
			q.pop();
			sccno[v] = scccnt;
		}
	}
}
void sol() {
	ans = num = 0;
	_rep(i, 1, n) {
		if (!dfn[i]) {
			tarjin(i);
		}
	}
	for (int i = 0; i < cnt; i++) {
		int u = G[i].u, v = G[i].v;
		if (sccno[u] != sccno[v]) {
			indegree[sccno[v]]++;
		}
	}
	_rep(i, 1, scccnt) {
		if (indegree[i] == 0) {
			ans++;
			num++;
		}
	}
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		init();
		read();
		sol();
		printf("%d\n", num);
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值