Skip to content

Commit 1145a45

Browse files
committed
Merge branch '10.0.x' of https://github.com/doubtfire-lms/doubtfire-api into 10.0.x
2 parents 54f71dc + 6913165 commit 1145a45

File tree

59 files changed

+8203
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+8203
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import java.util.*;
2+
3+
class Social{
4+
5+
public static Stack<Integer> s = new Stack<Integer>();
6+
public static int ctr = 0;
7+
public static int[] flood;
8+
9+
public static void printGraph(ArrayList<ArrayList<Integer>> g){
10+
for(int i=1; i<g.size(); i++){
11+
System.out.print(i+" -> ");
12+
for(int j=0; j<g.get(i).size(); j++)
13+
System.out.print(g.get(i).get(j)+" ");
14+
System.out.println();
15+
}
16+
}
17+
18+
public static void dfs(ArrayList<ArrayList<Integer>> g){
19+
int v[] = new int[g.size()];
20+
for(int i=1; i<g.size(); i++)
21+
if(v[i]==0)
22+
runDFS(g,i,v);
23+
}
24+
25+
public static void runDFS(ArrayList<ArrayList<Integer>> g, int n, int[] v){
26+
v[n]=1;
27+
for(int i=0; i<g.get(n).size(); i++)
28+
if(v[g.get(n).get(i)]==0)
29+
runDFS(g,g.get(n).get(i),v);
30+
if(!s.contains(n))
31+
s.push(n);
32+
}
33+
34+
public static ArrayList<ArrayList<Integer>> transpose(ArrayList<ArrayList<Integer>> g){
35+
ArrayList<ArrayList<Integer>> s = new ArrayList<ArrayList<Integer>>();
36+
for(int i=0; i<g.size(); i++)
37+
s.add(new ArrayList<Integer>());
38+
39+
for(int i=0; i<g.size(); i++)
40+
for(int j=0; j<g.get(i).size(); j++)
41+
s.get(g.get(i).get(j)).add(i);
42+
return s;
43+
}
44+
45+
public static void solve(ArrayList<ArrayList<Integer>> g){
46+
flood = new int[g.size()];
47+
while(!s.isEmpty()){
48+
int n = s.pop();
49+
if(flood[n]==0){
50+
ctr++;
51+
solve_2(g,n,flood);
52+
}
53+
}
54+
}
55+
56+
public static void solve_2(ArrayList<ArrayList<Integer>> g, int n, int[] flood){
57+
flood[n]=ctr;
58+
for(int i=0; i<g.get(n).size(); i++)
59+
if(flood[g.get(n).get(i)]==0)
60+
solve_2(g,g.get(n).get(i),flood);
61+
}
62+
63+
public static void main(String[] args){
64+
65+
Scanner input = new Scanner(System.in);
66+
67+
int nrcases = input.nextInt();
68+
for(int k=0; k<nrcases; k++){
69+
System.out.println("Caso #"+(k+1));
70+
ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>();
71+
s = new Stack<Integer>();
72+
ctr = 0;
73+
74+
int nrnodes = input.nextInt();
75+
for(int i=0; i<nrnodes+1; i++)
76+
g.add(new ArrayList<Integer>());
77+
78+
for(int i=0; i<nrnodes; i++){
79+
int node = input.nextInt();
80+
int nrcons = input.nextInt();
81+
for(int j=0; j<nrcons; j++)
82+
g.get(node).add(input.nextInt());
83+
}
84+
//printGraph(g);
85+
dfs(g);
86+
g=transpose(g);
87+
solve(g);
88+
89+
Arrays.sort(flood);
90+
int groups = 0;
91+
int pplgro = 0;
92+
93+
for(int i=1; i<flood.length; i++){
94+
int nrppl = 0;
95+
for(int j=0; j<flood.length; j++)
96+
if(flood[j]==i)
97+
nrppl++;
98+
if(nrppl>=4){
99+
groups++;
100+
pplgro+=nrppl;
101+
}
102+
}
103+
System.out.println(groups+" "+(nrnodes-pplgro));
104+
}
105+
}
106+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import java.util.*;
2+
3+
class Sociologia {
4+
public static void main(String args[]){
5+
Scanner stdin = new Scanner(System.in);
6+
7+
int ncenarios = stdin.nextInt();
8+
for (int i = 0; i < ncenarios; i++) {
9+
analisaCenario(stdin, i);
10+
}
11+
}
12+
13+
static void analisaCenario(Scanner stdin, int indcenario) {
14+
int nalunos = stdin.nextInt();
15+
Grafo cenario = new Grafo(nalunos);
16+
for (int i = 0; i < nalunos; i++) {
17+
int id = stdin.nextInt() - 1;
18+
analisaAluno(cenario, id, stdin);
19+
}
20+
//System.out.println(cenario);
21+
System.out.printf("Caso #%d\n", indcenario + 1);
22+
contaGrupos(cenario);
23+
}
24+
25+
static void analisaAluno(Grafo cenario, int id, Scanner stdin) {
26+
int namigos = stdin.nextInt();
27+
for (int i = 0; i < namigos; i++) {
28+
int idamigo = stdin.nextInt() - 1;
29+
Vertex amigo = cenario.vertices[idamigo];
30+
cenario.vertices[id].arcosadj.add(new Arco(amigo));
31+
}
32+
}
33+
34+
static void contaGrupos(Grafo cenario) {
35+
int ngrupos4oumais = 0;
36+
int npessoasoutros = 0;
37+
Stack<Vertex> s = new Stack<Vertex>();
38+
while (s.size() < cenario.vertices.length) {
39+
Vertex u = findNonStacked(cenario);
40+
cenario.dfsVisit(u, s, 1, true);
41+
}
42+
43+
cenario.reverteArcos();
44+
//System.out.println(cenario);
45+
while (!s.isEmpty()) {
46+
Vertex u = s.pop();
47+
if (u.stacked == true) {
48+
int npessoasgrupo = cenario.dfsVisit(u, s, 1, false);
49+
if (npessoasgrupo >= 4)
50+
ngrupos4oumais++;
51+
else
52+
npessoasoutros = npessoasoutros + npessoasgrupo;
53+
}
54+
}
55+
System.out.printf("%d %d\n", ngrupos4oumais, npessoasoutros);
56+
}
57+
58+
static Vertex findNonStacked(Grafo cenario) {
59+
for (int i = 0; i < cenario.vertices.length; i++) {
60+
if (cenario.vertices[i].stacked == false)
61+
return cenario.vertices[i];
62+
}
63+
return null;
64+
}
65+
66+
static class Grafo {
67+
Vertex[] vertices;
68+
69+
Grafo(int n) {
70+
vertices = new Vertex[n];
71+
72+
for (int i = 0; i < n; i++) {
73+
vertices[i] = new Vertex(i);
74+
}
75+
}
76+
77+
public int dfsVisit(Vertex u, Stack<Vertex> s, int npessoasgrupo, boolean primvolta) {
78+
u.color = Vertex.Color.gray;
79+
for (int i = 0; i < u.arcosadj.size(); i++) {
80+
if (u.arcosadj.get(i).nofinal.color == Vertex.Color.white) {
81+
Vertex v = u.arcosadj.get(i).nofinal;
82+
npessoasgrupo++;
83+
npessoasgrupo = dfsVisit(v, s, npessoasgrupo, primvolta);
84+
}
85+
}
86+
u.color = Vertex.Color.black;
87+
if (primvolta == true) {
88+
s.push(u);
89+
u.stacked = true;
90+
}
91+
if (primvolta == false)
92+
u.stacked = false;
93+
return npessoasgrupo;
94+
}
95+
96+
public void reverteArcos() {
97+
for (int i = 0; i < this.vertices.length; i++) {
98+
List<Arco> toRemove = new LinkedList<Sociologia.Arco>();
99+
for (int j = 0; j < this.vertices[i].arcosadj.size(); j++) {
100+
Arco actual = this.vertices[i].arcosadj.get(j);
101+
if (actual.processed == false) {
102+
Arco novo = new Arco(this.vertices[i]);
103+
novo.processed = true;
104+
this.vertices[actual.nofinal.id].arcosadj.add(novo);
105+
toRemove.add(actual);
106+
}
107+
}
108+
109+
for (Arco arco : toRemove) {
110+
this.vertices[i].arcosadj.remove(arco);
111+
}
112+
}
113+
114+
for (int i = 0; i < this.vertices.length; i++) {
115+
this.vertices[i].color = Vertex.Color.white;
116+
}
117+
}
118+
119+
public String toString() {
120+
StringBuffer sb = new StringBuffer();
121+
for (Vertex v : this.vertices) {
122+
sb.append("v = " + v.id + "\n");
123+
for (Arco a : v.arcosadj) {
124+
sb.append("\t--> " + a.nofinal.id + "\n");
125+
}
126+
}
127+
128+
return sb.toString();
129+
}
130+
}
131+
132+
static class Vertex {
133+
enum Color {
134+
white, gray, black
135+
}
136+
137+
int id;
138+
Color color;
139+
boolean stacked;
140+
LinkedList<Arco> arcosadj;
141+
142+
Vertex(int i) {
143+
id = i;
144+
color = Color.white;
145+
stacked = false;
146+
arcosadj = new LinkedList<Arco>();
147+
}
148+
}
149+
150+
static class Arco {
151+
boolean processed;
152+
Vertex nofinal;
153+
154+
Arco(Vertex n) {
155+
processed = false;
156+
nofinal = n;
157+
}
158+
}
159+
160+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import java.util.*;
2+
3+
4+
class No{
5+
6+
int val,amigos;
7+
boolean visitado;
8+
LinkedList<Integer> adj;
9+
10+
No(int vali)
11+
{
12+
val=vali;
13+
visitado=false;
14+
adj= new LinkedList<Integer>();
15+
amigos=0;}
16+
17+
void addNo(int val)
18+
{
19+
20+
adj.addFirst(val);
21+
amigos++;
22+
23+
}
24+
}
25+
26+
class Grafo{
27+
No[] g;
28+
No[] gt;
29+
int grupos,pessoas,ptemp;
30+
LinkedList<Integer> tempos;
31+
32+
Grafo(Scanner in)
33+
{
34+
grupos=0;
35+
tempos= new LinkedList<Integer>();
36+
pessoas=in.nextInt();
37+
g=new No[pessoas+1];
38+
gt=new No[pessoas+1];
39+
40+
for (int i = 1;i<=pessoas;i++)
41+
{g[i]= new No(i);gt[i]= new No(i);}
42+
43+
for (int i = 0;i<pessoas;i++)
44+
{
45+
int ptemp=in.nextInt();
46+
int am=in.nextInt();
47+
for (int j=1;j<=am;j++)
48+
{
49+
int amactual=in.nextInt();
50+
g[ptemp].addNo(amactual);
51+
gt[amactual].addNo(ptemp);
52+
}
53+
}
54+
}
55+
void DFS(){
56+
for (int i=1;i<=pessoas;i++)
57+
{if (!g[i].visitado)
58+
{ DFSVisit(g[i]);}}
59+
60+
}
61+
void DFSVisit(No actual)
62+
{ actual.visitado=true;
63+
for (int cada : actual.adj)
64+
{ if (!g[cada].visitado) {DFSVisit(g[cada]);}}
65+
tempos.addFirst(actual.val);}
66+
67+
68+
void contar()
69+
{
70+
while (!tempos.isEmpty()){
71+
int agora=tempos.removeFirst();
72+
if (!gt[agora].visitado){
73+
gt[agora].visitado=true;
74+
int pessoast= contarf(agora);
75+
if (pessoast>=4){grupos++;pessoas= pessoas-pessoast;}}
76+
77+
}
78+
}
79+
80+
int contarf(int pai)
81+
{ int contagem=1;
82+
gt[pai].visitado=true;
83+
for (int adjac : gt[pai].adj)
84+
{ if (!gt[adjac].visitado)
85+
{ contagem=contagem+contarf(adjac);}
86+
}
87+
return contagem;
88+
}
89+
90+
}
91+
92+
class soci{
93+
94+
public static void main(String args[])
95+
96+
{
97+
98+
Scanner ler= new Scanner(System.in);
99+
int casos;
100+
casos=ler.nextInt();
101+
for (int i=0;i<casos;i++)
102+
{ Grafo um= new Grafo(ler);
103+
um.DFS();
104+
um.contar();
105+
System.out.println("Caso #" + (i+1) );
106+
System.out.println(um.grupos + " " + um.pessoas);}
107+
108+
}
109+
}
110+

0 commit comments

Comments
 (0)