yuanye15978 @ 2020-01-05 12:12:19
优化输入输出之后过了
Scanner速度慢、耗内存,手写一个替代
System.out没缓存,速度比较慢,加上缓存再输出
JAVA可能不太适合刷OJ,但是谁让我喜欢JAVA呢
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author Administrator
*/
public class Main {
static int N;
static int M;
static short[] nodes;
static Random random = new Random(System.currentTimeMillis());
private static void connectRoot(short i, short j) {
if (random.nextInt(2) == 0) {
nodes[i] = j;
} else {
nodes[j] = i;
}
}
private static void connect(short i, short j) {
short iroot = root(i);
short jroot = root(j);
connectRoot(iroot, jroot);
}
private static short root(short i) {
if (nodes[i] == i) {
return i;
} else {
nodes[i] = root(nodes[i]);
return nodes[i];
}
}
private static boolean connected(short i, short j) {
return root(i) == root(j);
}
private static BufferedOutputStream out = new BufferedOutputStream(System.out, 16 * 1024);
private static PrintWriter writer = (new PrintWriter(out));
private static void run(int type, short x, short y) {
if (type == 1) {
connect(x, y);
} else {
if (connected(x, y)) {
writer.append("Y\n");
} else {
writer.append("N\n");
}
}
}
private static void init() {
for (int i = 0; i < nodes.length; i++) {
nodes[i] = (short) i;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
M = scanner.nextInt();
nodes = new short[N];
init();
for (int i = 0; i < M; i++) {
int type = scanner.nextInt();
short x = (short) (scanner.nextShort() - 1);
short y = (short) (scanner.nextShort() - 1);
run(type, x, y);
}
writer.flush();
}
}
class Scanner {
private BufferedInputStream in;
int c;
boolean atBeginningOfLine;
public Scanner(InputStream stream) {
in = new BufferedInputStream(stream, 4 * 1024);
try {
atBeginningOfLine = true;
c = (char) in.read();
} catch (IOException e) {
c = -1;
}
}
public boolean hasNext() {
if (!atBeginningOfLine) {
throw new Error("hasNext only works " +
"after a call to nextLine");
}
return c != -1;
}
public String next() {
StringBuffer sb = new StringBuffer();
atBeginningOfLine = false;
try {
while (c <= ' ') {
c = in.read();
}
while (c > ' ') {
sb.append((char) c);
c = in.read();
}
} catch (IOException e) {
c = -1;
return "";
}
return sb.toString();
}
public String nextLine() {
StringBuffer sb = new StringBuffer();
atBeginningOfLine = true;
try {
while (c != '\n') {
sb.append((char) c);
c = in.read();
}
c = in.read();
} catch (IOException e) {
c = -1;
return "";
}
return sb.toString();
}
public int nextInt() {
String s = next();
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return 0;
}
}
public int nextShort() {
String s = next();
try {
return Short.parseShort(s);
} catch (NumberFormatException e) {
return 0;
}
}
public double nextDouble() {
return new Double(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public void useLocale(int l) {
}
}
by Meatherm @ 2020-01-05 12:15:45
讨论区题解,自裁,请(无慈悲)
by pocafup @ 2020-01-05 12:23:18
@yuanye15978 如果你还想要你的号的话,第一时间把帖子删了吧。新人不懂我理解,但是你这个号基本上不用要了。记住别在讨论发AC代码(我没举报你尽快)
by pocafup @ 2020-01-05 12:27:36
至于为啥,请看这里
https://www.luogu.com.cn/discuss/show/148950
by t162 @ 2020-01-05 12:52:01
楼上两位,没这么严重吧
by HoshinoTented @ 2020-01-05 13:15:18
Orz
by Mr_Greeper @ 2020-01-05 13:29:46
哦666呢
by Krising @ 2020-06-25 09:46:53
楼上那俩老正能量了,希望人有事