Skip to content

Commit b36b589

Browse files
authored
Adding Codechef September2020 Lunchtime (sastava007#9)
Added Codechef September 2020 Lunchtime.
1 parent 69b01f7 commit b36b589

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long int
3+
using namespace std;
4+
int gcd(int a, int b)
5+
{
6+
if (b == 0)
7+
return a;
8+
return gcd(b, a % b);
9+
}
10+
int main()
11+
{
12+
ios_base::sync_with_stdio(false);
13+
cin.tie(NULL);
14+
int tc,n,flag,v;
15+
cin>>tc;
16+
while(tc--)
17+
{
18+
flag=1;
19+
cin>>n;
20+
vector<int>a(n);
21+
vector<int>b(n);
22+
for(int i=0;i<n;i++)
23+
{
24+
a[i]=i+1;
25+
cin>>b[i];
26+
}
27+
for(int i=0;i<n;i++)
28+
{
29+
if(a[i]!=b[i] && b[i]!=1)
30+
{
31+
v=gcd(a[i],b[i]);
32+
if(v!=b[i])
33+
{
34+
flag=0;
35+
break;
36+
}
37+
}
38+
}
39+
if(flag==0)
40+
{
41+
cout<<"NO"<<endl;
42+
}
43+
else
44+
{
45+
cout<<"YES"<<endl;
46+
}
47+
}
48+
return 0;
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long int
3+
using namespace std;
4+
void dfs(vector<int>a[], int n, bool visited[], int &ans, bool root[])
5+
{
6+
visited[n]=true;
7+
for(int i=0;i<a[n].size();i++)
8+
{
9+
if(!visited[a[n][i]])
10+
{
11+
dfs(a,a[n][i],visited,ans,root);
12+
}
13+
else
14+
{
15+
if(root[ a[n][i] ]==true)
16+
{
17+
root[a[n][i]]=false;
18+
}
19+
else
20+
{
21+
ans++;
22+
}
23+
}
24+
}
25+
}
26+
int main()
27+
{
28+
ios_base::sync_with_stdio(false);
29+
cin.tie(NULL);
30+
int tc,n,x,y,ans;
31+
cin>>tc;
32+
while(tc--)
33+
{
34+
ans=0;
35+
cin>>n;
36+
vector<int>a[n+1];
37+
bool visited[n+1];
38+
bool root[n+1];
39+
for(int i=1;i<n;i++)
40+
{
41+
visited[i]=false;
42+
cin>>x>>y;
43+
a[x].push_back(y);
44+
}
45+
for(int i=0;i<=n;i++)
46+
{
47+
visited[i]=false;
48+
root[i]=false;
49+
}
50+
for(int i=1;i<=n;i++)
51+
{
52+
if(visited[i]==false)
53+
{
54+
root[i]=true;
55+
dfs(a,i,visited,ans,root);
56+
}
57+
}
58+
cout<<ans<<endl;
59+
}
60+
return 0;
61+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<bits/stdc++.h>
2+
#define ll long long int
3+
using namespace std;
4+
int main()
5+
{
6+
ios_base::sync_with_stdio(false);
7+
cin.tie(NULL);
8+
int tc,n,sum,x;
9+
cin>>tc;
10+
while(tc--)
11+
{
12+
sum=0;
13+
cin>>n;
14+
vector<int>a(n);
15+
for(int i=0;i<n;i++)
16+
{
17+
cin>>a[i];
18+
sum+=a[i];
19+
}
20+
if(sum>=0)
21+
{
22+
cout<<"YES"<<endl;
23+
}
24+
else
25+
{
26+
cout<<"NO"<<endl;
27+
}
28+
}
29+
return 0;
30+
}

0 commit comments

Comments
 (0)