private:
int N, M;
vector<int> adj[2 * MAX];
// vector pentru evidenta vizitarii
int vis[2 * MAX];
// vector pentru a retine cea mai mica val a timpului de viz
int min_vis[2 * MAX];
int scc[2 * MAX];
stack<int> stk;
vector<int> solution;
bool inStack[2 * MAX];
// timpul de vizitare si nr de componente conexe
int timer, sccCount;
bool pos= true;
void read() {
ifstream fin("scandal.in");
fin >> N >> M;
for (int i = 0; i < M; ++i) {
int x, y, c;
fin >> x >> y >> c;
// scad x si y ca sa folosesc indexarea de la 0 in vect mei
--x; --y;
/* codificare pt 2 noduri (2 stari pentru x si 2 pt y - invitat si
neinvitat) */
int x_inv = 2 * x, y_inv = 2 * y;
int x_noninv = x_inv + 1, y_noninv = y_inv + 1;
// leg muchiile in functie de reguli
switch (c) {
case 0:
// daca x nu vine, atunci vine y
adj[x_noninv].push_back(y_inv);
// daca y nu vine, atunci vine x
adj[y_noninv].push_back(x_inv);
break;
case 1:
// daca x nu vine atunci nu vine nici y
adj[x_noninv].push_back(y_noninv);
// daca y vine atunci vine si x
adj[y_inv].push_back(x_inv);
break;
case 2:
// daca x vine, atunci vine si y
adj[x_inv].push_back(y_inv);
// daca nu vine y, atunci nu vine nici x
adj[y_noninv].push_back(x_noninv);
break;
case 3:
// daca vine x, atunci y nu vine
adj[x_inv].push_back(y_noninv);
// daca vine y atunci x nu vine
adj[y_inv].push_back(x_noninv);
break;
}
}
fin.close();
}
void get_res() {
/* initializez toate valorile din vis si min_vis cu -1 pt ca nu
au fost vizitate */
for (int i = 0; i < 2 * N; ++i) {
min_vis[i] = -1;
}
for (int i = 0; i < 2 * N; ++i) {
vis[i] = -1;
}
timer = 0;
sccCount = 0;
for (int i = 0; i < 2 * N; ++i) {
// daca nu e vizitat aplic tarjan
if (vis[i] == -1)
tarjan_dfs(i);
}
for (int i = 0; i < N; ++i) {
// daca nu e contradictie adaug in solutie
if (scc[2 * i] < scc[2 * i + 1])
solution.push_back(i + 1);
/* verific sa nu fie in aceeasi componenta si starea invitat si
neinvitat pt acelasi nod */
if (scc[2 * i] == scc[2 * i + 1]) {
pos= false;
break;
}
}
}
void tarjan_dfs(int u) {
// marchez u ca vizitat in vis si min_vis
vis[u] = min_vis[u] = timer++;
// il adaug in stiva
stk.push(u);
inStack[u] = true;
// pentru fiecare vecin al lui u
for (int v : adj[u]) {
// daca nu a fost vizitat pornesc dfs din el
if (vis[v] == -1) {
tarjan_dfs(v);
// salvez minimul dintre nodul curent si vecin
min_vis[u] = min(min_vis[u], min_vis[v]);
} else if (inStack[v]) {
// salvez minimul dintre nodul curent si vecin
min_vis[u] = min(min_vis[u], vis[v]);
}
}
// daca e punct de articulatie
if (min_vis[u] == vis[u]) {
while (!stk.empty()) {
// formez componenta conexa din nodurile de pe stiva
int v = stk.top(); stk.pop();
inStack[v] = false;
scc[v] = sccCount;
if (v == u) break;
}
// incrementez nr de componente conexe
sccCount++;
}
}
// functie pentru printarea rezultatului in fisier
void print() {
ofstream fout("scandal.out");
// daca exista vreo componenta buna printez componenta
if (pos) {
fout << solution.size() << "\n";
for (int person : solution) {
fout << person << "\n";
}
} else {
fout << "0\n";
}
fout.close();
}
The provided code solves a problem using a graph-based algorithm, specifically using Tarjan's algorithm to find strongly connected components (SCCs) in a directed graph. Here's how the complexity of this program breaks down:
-
Reading Input: The input involves reading (N) vertices and (M) edges from a file. The process of reading each of the (M) edges is (O(1)), so the total complexity for reading input is (O(M)).
-
Graph Construction: The code constructs the graph with (N \times 2) vertices and potentially additional edges based on conditions. The worst-case scenario is having (O(M)) edges since each input relation can generate at most up to four edges per condition. Therefore, this step has a complexity of (O(M)).
-
Tarjan's Algorithm:
- Initialization: This involves setting initial values for (2 \times N) vertices, which is (O(N)).
- Depth First Search (DFS) Execution: Tarjan's algorithm performs a DFS on each of (2N) vertices. DFS itself is (O(V + E)) where (V) is the number of vertices and (E) is the number of edges. Since each edge is visited twice (once for each direction), and counting the number of vertices, the complexity, in this case, would be (O(2N + 2M)) or simply (O(N+M)).
- SCC Detection: During DFS, SCCs are identified using a stack, with each node and accompanying operations (push/pop) performed within the DFS process. The complexity remains within the limits of DFS traversal, which is already included in the DFS complexity estimate above.
-
Solution Computation and Validity Check: After determining SCCs, a loop runs to check if any node and its complementary state (invited and not invited) are in the same SCC, which takes (O(N)).
-
Output: Writing the output involves listing the solution vertices. In the worst case, this would involve writing (N) vertices, so it has complexity (O(N)).
Overall Complexity: Summing up the complexities, the major contributing factor is the DFS traversal, and the overall time complexity of the program is (O(N + M)), where (N) is the number of guests (multiplied by 2 for two possible states per guest) and (M) is the number of relations or edges in the input.
This complexity signifies that both the number of vertices and the number of edges directly affect the performance of the algorithm, and it scales linearly with the increase in input size in terms of vertices and edges. This is efficient for large inputs where both (N) and (M) are significant.