CSAcademy Check DFS
/*
Verdict: Accepted
** One of Interesting problem I have ever seen by 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 = 1e5+3;
int visited[high] , order[high] , p[high];
vii adj[high] , dfsOrder;
void cln()
{
for(int i=0; i<high; i++)
{
adj[i].clr;
visited[i] = 0;
order[i] = 0;
}
dfsOrder.clr;
}
bool cmp(int a , int b)
{
return order[a] < order[b];
}
void DFS(int u)
{
visited[u] = 1;
dfsOrder.psb(u);
int len = adj[u].size() , v=0;
for(int i=0; i<len; i++)
{
v = adj[u][i];
if(!visited[v])
{
DFS(v);
}
}
}
int main()
{
fast;
int i , j , u , v , N , M , x;
while(cin >> N >> M)
{
cln();
for(i=1; i<=N; i++)
{
cin >> x;
order[x] = i;
p[i] = x;
}
for(i=0; i<M; i++)
{
cin >> u >> v;
adj[u].psb(v);
adj[v].psb(u);
}
for(i=1; i<=N; i++)
{
int len = adj[i].size();
for(j=0; j<len; j++)
{
sort(adj[i].begin(), adj[i].end() , cmp);
}
}
DFS(1); //outn("\n");
bool fl=true;
for(i=1; i<=N; i++)
{
if(p[i] != dfsOrder[i-1])
{
fl=false;
break;
}
}
if(fl) outn(1);
else outn(0);
// for(i=1; i<=N; i++)
// {
// cout << "for node " << i << ": ";
//
// for(j=0; j<adj[i].size(); j++)
// {
// cout << adj[i][j] << "; ";
// }
//
// cout << '\n';
// }
}
return 0;
}
Comments
Post a Comment