题目背景
本题和 金组同名题目 在题意上一致,唯一的不同是数据范围。
题目描述
FJ 和他的奶牛们正在计划离开小镇做一次长的旅行,同时 FJ 想临时地关掉他的农场以节省一些金钱。
这个农场一共有被用 MMM 条双向道路连接的 NNN 个谷仓(1≤N,M≤30001 \leq N,M \leq 30001≤N,M≤3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。
FJ 现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之前的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。
输入格式
输入第一行两个整数 N,MN,MN,M。
接下来 MMM 行,每行两个整数 u,vu,vu,v(1≤u,v≤N1 \leq u,v \leq N1≤u,v≤N),描述一条连接 u,vu,vu,v 两个农场的路。
最后 NNN 行每行一个整数,表示第 iii 个被关闭的农场编号。
输出格式
输出 NNN 行,每行包含 YES 或 NO,表示某个时刻农场是否是全连通的。
第一行输出最初的状态,第 iii 行(2≤i≤N2 \leq i \leq N2≤i≤N)输出第 i−1i-1i−1 个农场被关闭后的状态。
输入输出样例
输入 #1
4 3
1 2
2 3
3 4
3
4
1
2
输出 #1
YES
NO
YES
YES
题意 :
给定一张图,每次删除一个点,删除之前询问原图是否联通,一边删点一边判联通
题解;
- 判联通可以并查集,但是正向做很难搞,因为并查集不支持
Ctrl+Z
操作 - 考虑倒过来做,每次加入新点
V
,设U
是已经加入
且和V
有连边
的点,则连接U-V
( m e r g e ( u , v ) merge(u,v) merge(u,v))并判断联通块个数,记录答案即可
核心代码
read(n, m);
for(int i=1; i<=n; i++) pre[i] = i;
int u, v;
for(int i=1; i<=m; i++) { //记录所有边
read(u, v);
G[u].push_back(v), G[v].push_back(u);
}
for(int i=1; i<=n; i++) read(a[i]);
int cnt = 0;
for(int i=n; i>=1; i--) { //倒着插入新点并连边
int u = a[i];
vis[u] = true; //标记为已插入
cnt ++; //插入新点,联通块个数增加 1
for(auto v : G[u]) {
if(!vis[v]) continue ;
cnt -= union_xy(u, v);
}
ans[i] = (cnt == 1); //联通块个数仍为 1 说明删掉u仍然联通
}
for(int i=1; i<=n; i++) printf("%s\n", ans[i] ? "YES" : "NO");
完整c++
#define debug
#ifdef debug
#include <time.h>
#endif
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)2e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)
using namespace std;
#define show(x...) \
do { \
cout << "\033[31;1m " << #x << " -> "; \
err(x); \
} while (0)
void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
namespace FastIO{
char print_f[105];
void read() {}
void print() { putchar('\n'); }
template <typename T, typename... T2>
inline void read(T &x, T2 &... oth) {
x = 0;
char ch = getchar();
ll f = 1;
while (!isdigit(ch)) {
if (ch == '-') f *= -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) {
ll p3=-1;
if(x<0) putchar('-'), x=-x;
do{
print_f[++p3] = x%10 + 48;
} while(x/=10);
while(p3>=0) putchar(print_f[p3--]);
putchar(' ');
print(oth...);
}
} // namespace FastIO
using FastIO::print;
using FastIO::read;
int n, m, Q, K, a[MAXN], vis[MAXN], ans[MAXN];
vector<int> G[MAXN];
int pre[MAXN];
int fa(int x) { return pre[x]==x ? x : (pre[x]=fa(pre[x]));}
int union_xy(int x, int y) {
x = fa(x), y = fa(y);
if(x ^ y) { pre[y] = x; return true; }
return false;
}
signed main() {
#ifdef debug
freopen("test.txt", "r", stdin);
clock_t stime = clock();
#endif
read(n, m);
for(int i=1; i<=n; i++) pre[i] = i;
int u, v;
for(int i=1; i<=m; i++) { //记录所有边
read(u, v);
G[u].push_back(v), G[v].push_back(u);
}
for(int i=1; i<=n; i++) read(a[i]);
int cnt = 0;
for(int i=n; i>=1; i--) { //倒着插入新点并连边
int u = a[i];
vis[u] = true; //标记为已插入
cnt ++; //插入新点,联通块个数增加 1
for(auto v : G[u]) {
if(!vis[v]) continue ;
cnt -= union_xy(u, v);
}
ans[i] = (cnt == 1); //联通块个数仍为 1 说明删掉u仍然联通
}
for(int i=1; i<=n; i++) printf("%s\n", ans[i] ? "YES" : "NO");
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}
完整java
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static final boolean debug = true;
public static String INPATH = "C:\\Users\\majiao\\Desktop\\test.txt",
OUTPATH = "C:\\Users\\majiao\\Desktop\\out.txt";
public static StreamTokenizer tok;
public static BufferedReader cin;
public static PrintWriter cout;
public static long start_time = 0, out_time = 0;
public static int n, m, K, Q, MAXN = (int)2e5+7, INF = 0x3f3f3f3f,
pre[] = new int[MAXN], sum[] = new int[MAXN], POS = 50000,
a[] = new int[MAXN];
public static byte buf[] = new byte[MAXN];
public static int fa(int x) { return pre[x]==x ? x : (pre[x]=fa(pre[x])); }
public static int union_xy(int x, int y) {
x = fa(x); y = fa(y);
if(x != y) {
sum[x] += sum[y];
pre[y] = x;
return 1;
}
return 0;
}
static class Edge implements Comparable<Edge> {
int u, v, w;
@Override
public int compareTo(Edge o) { return w - o.w; }
}
// static Edge a[] = new Edge[MAXN];
static List<Integer> G[] = new ArrayList[MAXN];
public static void main(String[] args) throws IOException {
main_init();
if(debug) { start_time = System.currentTimeMillis(); }
if(false) { System.setOut(new PrintStream(OUTPATH)); }
n = read_int(); m = read_int();
for(int i=0; i<=n; i++) {
G[i] = new ArrayList<>();
pre[i] = i;
}
boolean vis[] = new boolean[MAXN];
for(int i=1; i<=m; i++) {
int u, v;
u = read_int(); v = read_int();
G[u].add(v); G[v].add(u);
}
for(int i=1; i<=n; i++) a[i] = read_int();
int cnt = 0;
boolean ans[] = new boolean[MAXN];
for(int i=n; i>=1; i--) {
int u = a[i];
vis[u] = true;
cnt ++;
for (Integer v : G[u]) {
if(!vis[v]) continue;
cnt -= union_xy(u, v);
}
ans[i] = (cnt == 1);
}
for(int i=1; i<=n; i++)
cout.printf("%s\n", ans[i] ? "YES" : "NO");
if(debug) {
out_time = System.currentTimeMillis();
cout.printf("run time : %d ms\n", out_time-start_time);
}
cout.flush();
}
public static void show(List<Object> list, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
for(Object x : list) {
cout.printf("[%s] ", x);
}
cout.printf("\n");
}
public static void show(Map<Object, Object> mp, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
Set<Map.Entry<Object, Object>> entries = mp.entrySet();
for (Map.Entry<Object, Object> en : entries) {
cout.printf("[%s,%s] ", en.getKey(), en.getValue());
}
cout.printf("\n");
}
public static<T> void forarr(T arr[], int ...args) {
int lef = 0, rig = arr.length - 1;
if(args.length > 0) { lef = args[0]; rig = args[1]; }
cout.printf(" : ");
for( ; lef<=rig; lef++) {
cout.printf("[%s] ", args[lef]);
}
cout.printf("\n");
}
public static void main_init() {
try {
if (debug) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream(INPATH)));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
// cout = new PrintWriter(OUTPATH);
tok = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String next_str() {
try {
tok.nextToken();
if (tok.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tok.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tok.nval);
} else if (tok.ttype == StreamTokenizer.TT_WORD) {
return tok.sval;
} else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static int read_int() {
String tmp_next_str = next_str();
return null==tmp_next_str ? -1 : Integer.parseInt(tmp_next_str);
}
public static long read_long() { return Long.parseLong(next_str()); }
public static double read_double() { return Double.parseDouble(next_str()); }
public static BigInteger read_big() { return new BigInteger(next_str()); }
public static BigDecimal read_dec() { return new BigDecimal(next_str()); }
}