UVa 13038 - Directed Forest
/*
Verdict: Accepted
Solution Technique: Longest/Maximum depth from root
Algorithm used: 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 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
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;
vii adj[high];
int indegree[high];
void clr(int len)
{
for(int i=0; i<=len; i++)
{
adj[i].clear();
indegree[i] = 0;
}
}
int DFS(int u)
{
int mxdepth = 1 , len = adj[u].size() , depth=0;
//cout << "u = " << u << " depth = " << depth << "\n";
for(int i=0; i<len; i++)
{
int v = adj[u][i];
//cout << "v = " << v << " befor mxdepth = " << mxdepth << "\n";
depth = DFS(v) + 1;
//cout << " v = " << v << " depth = " << depth << "\n";
mxdepth = max(depth , mxdepth);
//cout << "v = " << v << " mxdepth = " << mxdepth << "\n";
}
return mxdepth;
}
int main()
{
//fast;
//freopen("out.txt", "w" , stdout);
int i , u , v , test, tc=0 , N , E , ans=0;
sf("%d", &test);
while(test--)
{
ans = 0;
sf("%d %d", &N , &E);
clr(N);
for(i=0; i<E; i++)
{
sf("%d %d", &u , &v);
adj[u].psb(v);
indegree[v]++;
}
for(i=1; i<=N; i++)
{
if(!indegree[i])
{
ans = max(DFS(i) , ans);
//cout << " maximum = " << ans << "\n";
}
}
pf("Case %d: %d\n", ++tc, ans);
}
return 0;
}
Verdict: Accepted
Solution Technique: Longest/Maximum depth from root
Algorithm used: 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 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
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;
vii adj[high];
int indegree[high];
void clr(int len)
{
for(int i=0; i<=len; i++)
{
adj[i].clear();
indegree[i] = 0;
}
}
int DFS(int u)
{
int mxdepth = 1 , len = adj[u].size() , depth=0;
//cout << "u = " << u << " depth = " << depth << "\n";
for(int i=0; i<len; i++)
{
int v = adj[u][i];
//cout << "v = " << v << " befor mxdepth = " << mxdepth << "\n";
depth = DFS(v) + 1;
//cout << " v = " << v << " depth = " << depth << "\n";
mxdepth = max(depth , mxdepth);
//cout << "v = " << v << " mxdepth = " << mxdepth << "\n";
}
return mxdepth;
}
int main()
{
//fast;
//freopen("out.txt", "w" , stdout);
int i , u , v , test, tc=0 , N , E , ans=0;
sf("%d", &test);
while(test--)
{
ans = 0;
sf("%d %d", &N , &E);
clr(N);
for(i=0; i<E; i++)
{
sf("%d %d", &u , &v);
adj[u].psb(v);
indegree[v]++;
}
for(i=1; i<=N; i++)
{
if(!indegree[i])
{
ans = max(DFS(i) , ans);
//cout << " maximum = " << ans << "\n";
}
}
pf("Case %d: %d\n", ++tc, ans);
}
return 0;
}
Comments
Post a Comment