</pre></h1><h1 style="margin:0px 0px 0.5em; padding:0px; border:0px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; font-size:3em; font-weight:normal; line-height:1em; vertical-align:baseline; color:rgb(17,17,17); background-color:rgb(250,250,250)"><pre style="margin-top:1.5em; margin-bottom:1.5em; padding:0px; border:0px; font-family:'Droid Sans Mono',Consolas,'Courier New',monospace; vertical-align:baseline; overflow:auto"><span style="font-size:18px"><span style="line-height:27px"></span></span><pre code_snippet_id="1914188" snippet_file_name="blog_20161006_2_3434556" name="code" class="java"><pre code_snippet_id="1914188" snippet_file_name="blog_20161006_1_4371864" name="code" class="java" style="color: rgb(17, 17, 17); font-size: 36px; line-height: 36px;">题目的意思就是求有多少个联通分量,首先想到的是用DFS,于是用Java写了一下:
package p1013;
import java.util.Scanner;
/*
* 可以更优化
*/
public class Copy_2_of_Main {
static boolean[] marked;
static int[] cc;
static String[] connected;
static int N;
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
N = sc.nextInt();
int M = sc.nextInt();
int Q = sc.nextInt();
//marked = new boolean[1+N];
cc = new int[1+N];
connected = new String[1+N];
for(int i=0; i<M; i++) {
int s = sc.nextInt();
int t = sc.nextInt();
if(connected[s] == null)
connected[s] = String.valueOf(t);
else
connected[s] += String.valueOf(t);
//connected[t][s] = true;
}
for(int i=0; i<Q; i++) {
System.out.println(CC());
}
}
private static int CC() {
marked = new boolean[1+N];
marked[sc.nextInt()] = true;
int cnt = 0;
for(int i=1; i<=N; i++) {
if(!marked[i]) {
dfs(i);
cnt ++;
}
}
return cnt - 1;
}
private static void dfs(int start) {
marked[start] = true;
for(int i=1; i<=N; i++) {
if(!marked[i] && (connected[start] != null || connected[i] != null)) {
if(connected[start].indexOf(i+"") != -1 || connected[i].indexOf(start+"") != -1)
dfs(i);
}
}
}
}
然而最后一个用例超时了,考虑到每次查一次都要DFS遍历整个图,其中很多都是重复的,所以改进了一下:
package p1013;
import java.util.Scanner;
/*
* 可以更优化
*/
public class Main {
static boolean[] marked;
static int[] cc;
static boolean[][] connected;
static int N;
static int s, sid;
static int cnt = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
int M = sc.nextInt();
int Q = sc.nextInt();
//marked = new boolean[1+N];
cc = new int[1+N];
connected = new boolean[1+N][1+N];
for(int i=0; i<M; i++) {
int s = sc.nextInt();
int t = sc.nextInt();
connected[s][t] = true;
connected[t][s] = true;
}
int all = CC();
for(int i=0; i<Q; i++) {
s = sc.nextInt();
sid = cc[s];
System.out.println(all - 2 + CC2());
}
}
private static int CC2() {
cnt = 0;
marked = new boolean[1+N];
for(int i=1; i<=N; i++) {
if(i != s && cc[i] == sid && !marked[i]) {
dfs2(i);
cnt ++;
}
}
return cnt;
}
private static void dfs2(int start) {
marked[start] = true;
for(int i=1; i<=N; i++) {
if(i != s && cc[i] == sid && !marked[i] && connected[start][i]) {
dfs(i);
}
}
}
private static int CC() {
cnt = 0;
marked = new boolean[1+N];
for(int i=1; i<=N; i++) {
if(!marked[i]) {
dfs(i);
cnt ++;
}
}
return cnt;
}
private static void dfs(int start) {
marked[start] = true;
cc[start] = cnt;
for(int i=1; i<=N; i++) {
if(!marked[i] && connected[start][i]) {
dfs(i);
}
}
}
}
然而还是最后一个用例不通过,然后想到可能是Java运行本来就慢,于是换C++,艰难的照着网上的答案和自己的算法思路一样的敲了一遍,全部AC,而且时间比Java不是一般的少(最后一个用例也是特别变态):
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX 1000
// 会初始化?
int n, m, k;
bool marked[MAX];
bool connected[MAX][MAX];
void dfs(int t) {
marked[t] = true;
for(int i=1; i<=n; i++) {
if(!marked[i] && connected[t][i]) {
dfs(i);
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
int a, b;
for(int i=0; i<m; i++) {
scanf("%d%d", &a, &b);
connected[a][b] = true;
connected[b][a] = true;
}
int c;
while(k --) {
int count = 0;
scanf("%d", &c);
memset(marked, false, sizeof(marked));
marked[c] = true;
for(int i=1; i<=n; i++){
if(!marked[i]) {
dfs(i);
count++;
}
}
printf("%d\n", count-1);
}
return 0;
}
测试点
</pre></h1><h1 style="margin:0px 0px 0.5em; padding:0px; border:0px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; font-size:3em; font-weight:normal; line-height:1em; vertical-align:baseline; color:rgb(17,17,17); background-color:rgb(250,250,250)"><pre style="margin-top:1.5em; margin-bottom:1.5em; padding:0px; border:0px; font-family:'Droid Sans Mono',Consolas,'Courier New',monospace; vertical-align:baseline; overflow:auto"><span style="font-size:18px"><span style="line-height:27px"></span></span><pre code_snippet_id="1914188" snippet_file_name="blog_20161006_2_3434556" name="code" class="java"><pre code_snippet_id="1914188" snippet_file_name="blog_20161006_1_4371864" name="code" class="java" style="color: rgb(17, 17, 17); font-size: 36px; line-height: 36px;">题目的意思就是求有多少个联通分量,首先想到的是用DFS,于是用Java写了一下:
package p1013;
import java.util.Scanner;
/*
* 可以更优化
*/
public class Copy_2_of_Main {
static boolean[] marked;
static int[] cc;
static String[] connected;
static int N;
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
N = sc.nextInt();
int M = sc.nextInt();
int Q = sc.nextInt();
//marked = new boolean[1+N];
cc = new int[1+N];
connected = new String[1+N];
for(int i=0; i<M; i++) {
int s = sc.nextInt();
int t = sc.nextInt();
if(connected[s] == null)
connected[s] = String.valueOf(t);
else
connected[s] += String.valueOf(t);
//connected[t][s] = true;
}
for(int i=0; i<Q; i++) {
System.out.println(CC());
}
}
private static int CC() {
marked = new boolean[1+N];
marked[sc.nextInt()] = true;
int cnt = 0;
for(int i=1; i<=N; i++) {
if(!marked[i]) {
dfs(i);
cnt ++;
}
}
return cnt - 1;
}
private static void dfs(int start) {
marked[start] = true;
for(int i=1; i<=N; i++) {
if(!marked[i] && (connected[start] != null || connected[i] != null)) {
if(connected[start].indexOf(i+"") != -1 || connected[i].indexOf(start+"") != -1)
dfs(i);
}
}
}
}
package p1013;
import java.util.Scanner;
/*
* 可以更优化
*/
public class Main {
static boolean[] marked;
static int[] cc;
static boolean[][] connected;
static int N;
static int s, sid;
static int cnt = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
int M = sc.nextInt();
int Q = sc.nextInt();
//marked = new boolean[1+N];
cc = new int[1+N];
connected = new boolean[1+N][1+N];
for(int i=0; i<M; i++) {
int s = sc.nextInt();
int t = sc.nextInt();
connected[s][t] = true;
connected[t][s] = true;
}
int all = CC();
for(int i=0; i<Q; i++) {
s = sc.nextInt();
sid = cc[s];
System.out.println(all - 2 + CC2());
}
}
private static int CC2() {
cnt = 0;
marked = new boolean[1+N];
for(int i=1; i<=N; i++) {
if(i != s && cc[i] == sid && !marked[i]) {
dfs2(i);
cnt ++;
}
}
return cnt;
}
private static void dfs2(int start) {
marked[start] = true;
for(int i=1; i<=N; i++) {
if(i != s && cc[i] == sid && !marked[i] && connected[start][i]) {
dfs(i);
}
}
}
private static int CC() {
cnt = 0;
marked = new boolean[1+N];
for(int i=1; i<=N; i++) {
if(!marked[i]) {
dfs(i);
cnt ++;
}
}
return cnt;
}
private static void dfs(int start) {
marked[start] = true;
cc[start] = cnt;
for(int i=1; i<=N; i++) {
if(!marked[i] && connected[start][i]) {
dfs(i);
}
}
}
}
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX 1000
// 会初始化?
int n, m, k;
bool marked[MAX];
bool connected[MAX][MAX];
void dfs(int t) {
marked[t] = true;
for(int i=1; i<=n; i++) {
if(!marked[i] && connected[t][i]) {
dfs(i);
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
int a, b;
for(int i=0; i<m; i++) {
scanf("%d%d", &a, &b);
connected[a][b] = true;
connected[b][a] = true;
}
int c;
while(k --) {
int count = 0;
scanf("%d", &c);
memset(marked, false, sizeof(marked));
marked[c] = true;
for(int i=1; i<=n; i++){
if(!marked[i]) {
dfs(i);
count++;
}
}
printf("%d\n", count-1);
}
return 0;
}
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 3 | 384 | 7/7 |
1 | 答案正确 | 4 | 256 | 8/8 |
2 | 答案正确 | 5 | 244 | 3/3 |
3 | 答案正确 | 5 | 1408 | 3/3 |
4 | 答案正确 | 137 | 1408 | 4/4 |
Java的什么Scanner啊这些好像都比scanf慢多了。。。。
网上说还有一种并查集的解法,到时候再试一下,现在是在是无力吐槽这个PAT了