URI 1610 Dudu Service Maker
/**
* @Author: Pranta Sarker
*
**/
// Cycle finding Problem
// Used Algorithm: DFS
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 30002;
const int gray = 1;
const int black = -1;
const int white = 0;
bool cycle = false;
vii adj[high];
int color[high];
void CLR()
{
loop0(high)
{
adj[i].clr;
color[i] = white;
}
cycle = false;
}
void DFS(int u)
{
if(color[u] == gray)
{
cycle = true;
return;
}
color[u] = gray;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(color[v] == white)
{
DFS(v);
}
else if(color[v] == gray)
{
cycle = true;
return;
}
}
color[u] = black;
}
int main()
{
fast;
int test;
cin >> test;
while(test--)
{
CLR();
int i , N , M, u , v;
cin >> N >> M;
for(i=0; i<M; i++)
{
cin >> u >> v;
adj[u].psb(v);
}
for(i=1; i<=N; i++)
{
if(color[i] == white)
{
DFS(i);
}
else break;
}
if(cycle) cout << "SIM\n";
else cout << "NAO\n";
}
return 0;
}
* @Author: Pranta Sarker
*
**/
// Cycle finding Problem
// Used Algorithm: DFS
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const int high = 30002;
const int gray = 1;
const int black = -1;
const int white = 0;
bool cycle = false;
vii adj[high];
int color[high];
void CLR()
{
loop0(high)
{
adj[i].clr;
color[i] = white;
}
cycle = false;
}
void DFS(int u)
{
if(color[u] == gray)
{
cycle = true;
return;
}
color[u] = gray;
for(int i=0; i<adj[u].size(); i++)
{
int v = adj[u][i];
if(color[v] == white)
{
DFS(v);
}
else if(color[v] == gray)
{
cycle = true;
return;
}
}
color[u] = black;
}
int main()
{
fast;
int test;
cin >> test;
while(test--)
{
CLR();
int i , N , M, u , v;
cin >> N >> M;
for(i=0; i<M; i++)
{
cin >> u >> v;
adj[u].psb(v);
}
for(i=1; i<=N; i++)
{
if(color[i] == white)
{
DFS(i);
}
else break;
}
if(cycle) cout << "SIM\n";
else cout << "NAO\n";
}
return 0;
}
Comments
Post a Comment