Pagini recente » Cod sursa (job #1171417) | Cod sursa (job #212410) | Cod sursa (job #2735666) | Cod sursa (job #1802439) | Cod sursa (job #2928626)
#include <bits/stdc++.h>
using namespace std;
/// leetcode 886 - 1 a
// O(n * m) time complexity
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
vector<int> viz;
viz.resize(n + 1);
vector<vector<int>> lista;
lista.resize(n + 1);
for (auto &node : dislikes) {
lista[node[0]].push_back(node[1]);
lista[node[1]].push_back(node[0]);
}
for (int node = 1; node <= n; node++) {
if (viz[node] == 0) {
queue<int> coada;
coada.push(node);
viz[node] = 1;
while (!coada.empty()) {
int currentNode = coada.front();
coada.pop();
for (int &x : lista[currentNode]) {
if (viz[x] == 0) {
viz[x] = -viz[currentNode];
coada.push(x);
}
else if (viz[x] == viz[currentNode]) {
return false;
}
}
}
}
}
return true;
}
/// 1 - b
vector<vector<int>> possibleBipartition2(int n, vector<vector<int>>& dislikes) {
vector<int> viz;
viz.resize(n + 1);
vector<vector<int>> lista;
lista.resize(n + 1);
for (auto &node : dislikes) {
lista[node[0]].push_back(node[1]);
lista[node[1]].push_back(node[0]);
}
vector<vector<int>> ans;
ans.resize(2);
for (int node = 1; node <= n; node++) {
if (viz[node] == 0) {
queue<int> coada;
coada.push(node);
viz[node] = 1;
while (!coada.empty()) {
int currentNode = coada.front();
coada.pop();
for (int &x : lista[currentNode]) {
if (viz[x] == 0) {
viz[x] = -viz[currentNode];
coada.push(x);
}
else if (viz[x] == viz[currentNode]) {
return {};
}
}
}
}
}
for (int i = 1; i <= n; i++) {
if (viz[i] == 1) {
ans[0].push_back(i);
}
else {
ans[1].push_back(i);
}
}
return ans;
}
/// csa academy check-dfs - 2
// O(n+m) time complexity?
bool checkDfs(){
int n, m;
cin>>n>>m;
int p[n];
for(int i = 0; i < n; i++){
cin>>p[i];
}
vector<vector<int>> lista;
lista.resize(n + 1);
int x, y;
for(int i = 0; i < m; i++){
cin>>x>>y;
lista[x].push_back(y);
lista[y].push_back(x);
}
bool check;
int currentNode;
stack<int> stiva;
stiva.push(p[0]);
vector<int> vis;
vis.resize(n + 1);
vis[p[0]] = 1;
for(int i = 1; i < n; i++){
check = true;
while(!stiva.empty() and check){
currentNode = stiva.top();
int cnt = 0;
for(int node : lista[currentNode]) {
if (node == p[i]) {
stiva.push(node);
check = false;
break;
}
cnt+=vis[node];
}
if(check)
{
if (cnt < lista[currentNode].size()) return false;
stiva.pop();
}
}
if(stiva.empty()) return false;
vis[p[i]] = 1;
}
return true;
}
///leetcode 210 - 3 a
// stack<int> stiva;
// vector<int> viz;
// vector<int> stivaCurenta;
// vector<vector<int>> lista;
// bool isPossible = true;
// void DFS (int node){
// for(int &x : lista[node]){
// if(!isPossible) return;
// if(stivaCurenta[x]){
// isPossible = false;
// return;
// }
// if(!viz[x]){
// stivaCurenta[x] = 1;
// viz[x] = 1;
// DFS(x);
// stivaCurenta[x] = 0;
// }
// }
// stiva.push(node);
// }
//
// vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
// viz.resize(numCourses);
// lista.resize(numCourses);
// stivaCurenta.resize(numCourses);
// for(auto &x : prerequisites){
// lista[x[1]].push_back(x[0]);
// }
// for(int i = 0; i < numCourses; i++){
// if(!isPossible) return {};
// if(!viz[i]){
// viz[i] = 1;
// stivaCurenta[i] = 1;
// DFS(i);
// stivaCurenta[i] = 0;
// }
// }
// if(!isPossible) return {};
// vector<int> ans;
// while(!stiva.empty()){
// ans.push_back(stiva.top());
// stiva.pop();
// }
// return ans;
// }
/// 3 - b
//stack<int> stiva;
//vector<int> viz;
//stack<int> stivaCurenta;
//vector<int> stivaCurentaVector;
//vector<vector<int>> lista;
//bool isPossible = true;
//void DFS (int node){
// for(int &x : lista[node]){
// if(!isPossible) return;
// if(stivaCurentaVector[x]){
// isPossible = false;
// return;
// }
// if(!viz[x]){
// stivaCurenta.push(x);
// stivaCurentaVector[x] = 1;
// viz[x] = 1;
// DFS(x);
// if(isPossible)
// {
// stivaCurenta.pop();
// stivaCurentaVector[x] = 0;
// }
// }
// }
// stiva.push(node);
//}
//
//vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
// viz.resize(numCourses);
// lista.resize(numCourses);
// stivaCurentaVector.resize(numCourses);
// for(auto &x : prerequisites){
// lista[x[1]].push_back(x[0]);
// }
// for(int i = 0; i < numCourses; i++){
// if(!viz[i]){
// viz[i] = 1;
// stivaCurenta.push(i);
// stivaCurentaVector[i] = 1;
// DFS(i);
// if(!isPossible) {
// vector<int> ans;
// while(!stivaCurenta.empty()){
// ans.push_back(stivaCurenta.top());
// stivaCurenta.pop();
// }
// reverse(ans.begin(), ans.end());
// ans.push_back(ans[0]);
// return ans;
// }
// stivaCurenta.pop();
// stivaCurentaVector[i] = 0;
// }
// }
//
// return {};
//}
///infoarena ctc - 4
vector<vector<int>> lista;
vector<vector<int>> listaT;
vector<int> viz;
stack<int>stiva;
vector<int> ctc;
void DFS(int node){
viz[node] = 1;
for(int &x : lista[node]){
if(!viz[x]){
DFS(x);
}
}
stiva.push(node);
}
void DFST(int node, int ctcNumber){
viz[node] = 1;
ctc[node] = ctcNumber;
for(int &x : listaT[node]){
if(!ctc[x]){
DFST(x, ctcNumber);
}
}
}
void ex4(){
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m;
fin>>n>>m;
lista.resize(n + 1);
listaT.resize(n + 1);
viz.resize(n + 1);
ctc.resize(n + 1);
int x, y;
for(int i = 0; i < m; i++){
fin>>x>>y;
lista[x].push_back(y);
listaT[y].push_back(x);
}
for(int i = 1; i <= n; i++){
if(!viz[i]){
DFS(i);
}
}
int ctcNumber = 1;
while(!stiva.empty()){
int node = stiva.top();
stiva.pop();
if(!ctc[node]){
DFST(node, ctcNumber);
ctcNumber++;
}
}
vector<vector<int>> ans(ctcNumber - 1);
for(int i = 1; i <= n; i++){
ans[ctc[i] - 1].push_back(i);
}
fout<<ans.size()<<'\n';
for(auto &x : ans) {
for (int &y: x) {
fout << y << " ";
}
fout << "\n";
}
}
int main() {
// int n = 6;
// vector<vector<int>> dislikes = { {1,2},{1,3},{2, 4} };
// vector<vector<int>> ans = possibleBipartition2(n, dislikes);
// for (auto &x : ans) {
// for (auto &y : x) {
// cout << y << " ";
// }
// cout << endl;
// }
// cout<<checkDfs();
// int numCourses = 5;
// vector<vector<int>> prerequisites = { {1, 0}, {2, 1}, {3, 2}, {0, 3}};
//
// vector<int> ans = findOrder(numCourses, prerequisites);
// for(auto &x : ans){
// cout<<x<<" ";
// }
ex4();
return 0;
}