Using part of the union find algorithm. Order(n), linear space complexity. The OJ said beat 100% solution.I 've no idea is there anything wrong. Here is my code:

```
public class Solution {
public boolean canFinish(int n, int[][] p) {
int[] g = new int[n];
for(int i=0; i<n; i++) g[i] = -1;
for(int i=0; i<p.length; i++) {
int curIndex = p[i][0];
int rootIndex = p[i][1];
g[curIndex] = rootIndex;
while(g[rootIndex] != -1) {
if(g[rootIndex] == curIndex) return false;
g[curIndex] = g[rootIndex];
rootIndex = g[rootIndex];
}
}
return true;
}
```

}