publicclassMain{ privatestaticfinalint N = 1000001; privatestaticint[] x = newint[N]; privatestaticint[] y = newint[N]; privatestaticboolean[] z = newboolean[N]; privatestaticint[] fa = newint[N]; privatestaticint tot = 0; privatestatic Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
publicstaticvoidmain(String[] args){ Scanner in = new Scanner(System.in); int T = in.nextInt(); while(T-- > 0) { mp.clear(); Arrays.fill(x, 0); Arrays.fill(y, 0); Arrays.fill(z, false); tot = 0; for(int i = 1; i <= N - 1; i++) { fa[i] = i; } int n = in.nextInt(); for(int i = 1; i <= n; i++) { String s = in.next(); String mark = in.next(); String t = in.next(); for(int j = 1; j < s.length(); j++) { x[i] = (s.charAt(j) - '0') + x[i] * 10; } for(int j = 1; j < t.length(); j++) { y[i] = (t.charAt(j) - '0') + y[i] * 10; } /** * 如果写 (mark == "=" )永远为假 * 必须写 mark.equals("=") */ if(mark.equals("=") ) { //System.out.println("****"); z[i] = true; } else { z[i] = false; } } for(int i = 1; i <= n; i++) { x[i] = get(x[i]); y[i] = get(y[i]); } solve(n);
} } publicstaticintFind(int x){ int y = x; while(y != fa[y]) { y = fa[y]; } while(y != x) { int tmp = fa[x]; fa[x] = y; x = tmp; } return y; } publicstaticbooleanUnion(int x, int y){ x = Find(x); y = Find(y); if(x != y) { fa[y] = x; returntrue; } returnfalse; }