DAA ELAB Session 679
DAA ELAB Session 679
DAA ELAB Session 679
Given a chess board having NxN cells, you need to place N queens on the board in such
#include<iostream>
int n;
bool grid[10][10];
int i,j;
return true;
for(int col=0;col<n;++col){
if(isSafe(row,col)){
grid[row][col]=true;
grid[row][col]=false;
return false;
}
int main(){
cin>>n;
int i;
if(solveQueen(0)){
for(i=0;i<n;++i){
cout<<endl;
return 0;
#include<bits/stdc++.h>
void solve()
int x,y;
cin>>x>>y;
cout<<(x-y)+y/2<<"\n";
int main()
int t;
t=1;
while(t--)
solve();
#include<bits/stdc++.h>
double DP[MAXN][MAXW];
int main(){
int t;
cin>>t;
while(t--){
int X,Y,N,W,P1,P2;
cin>>X>>Y>>N>>W>>P1>>P2;
for(int i=0;i<=N;i++){
for(int j=0;j<=W;j++){
DP[i][j]=0;
DP[i][0] = 1;
DP[0][i] = 0;
printf("%.6f\n",DP[N][W]*100);
Given integer N, you need to find four integers A,B,C,D, such that they’re all factors
#include<bits/stdc++.h>
void solve(){}
int main(){
else cout<<"-1\n";
return 0;
bool flag;
int sum , n;
return;
int main()
int t;
cin >> t;
while(t--){
cin >> n;
sum = 0;
flag = false;
sum += arr[i];
else {
rec(0 , 0);
return 0;
You are given three arrays a1…n,b1…n,c1…n and two numbers M and K.
#include <iostream>
#include<bits/stdc++.h>
#define f1 for(i=0;i<n;i++)
long long int min(long long int x, long long int y){
if(x < y)
return x;
else
return y;
int main(){
int n, m, K;
int p,T = 0;
T=0;
T++;
if(T==K)
break;
}
if(l < min(rz,lz + m))
break;
break;
cout << i << " " << j << " " << l;
else
The end semester exams are now approaching. Ritu is the computer
#include<iostream>
#include<bits/stdc++.h>
int arr[1001][1001];
int dp[1001][1001];
void solve(){
cout<<"for(i=0;i<N;i++)";}
if(arr[i][j]==1) return 1;
dp[i][j] = a+b+c+d+1;
dp[i][j]%=mod;
return dp[i][j];
int main()
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n;
cin>>n;
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
cin>>arr[i][j];
memset(dp,-1,sizeof(dp));
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
count+=a;
count%=mod;
cout<<count<<'\n';
}
You are given two numbers n and k. for each number in the interval [1,n], your task is to
#include<bits/stdc++.h>
if (n == 0) return 0;
int main () {
int T = 1;
scanf("%d", &T);
while(T--) {
int n, k;
}
return 0;
Lucky numbers are defined as the numbers consisting only of digits 3 and 5.
#include<bits/stdc++.h>
string solve(string& s)
int n = s.size(), i = 0;
++i;
if(s[i] == '4')
s[i] = '5';
else
s[i]='3';
++i;
while(i<n)
s[i] = '3';
++i;
else
if(i < 0)
s[i] = '5';
++i;
while(i < n)
s[i] = '3';
++i;
return s;
int main()
int t;
cin >> t;
while(t--)
string s;
cin >> s;
}
SESSION 7: GRAPH COLOURING:-
Adiththi likes drawing. She has drawn many graphs already, both directed and not.
#include<bits/stdc++.h>
int i,j,x,y,n,m,ans,f[502],a[502];
scanf("%d %d",&n,&m);
for (i=1;i<=m;i++){
scanf("%d %d",&x,&y);a[x]++;a[y]++;
f[find(x)]=find(y);
for (i=1;i<=n;i++)
if (a[i]>2)
return printf("NO\n"),0;
printf("YES\n%d\n",n-m);
for (i=1;i<=n;i++)
while (a[i]<2){
ans=i+(n!=1);
for (j=ans;j<=n;j++)
if (a[j]<2&&(n<=2||m+1==n||find(i)!=find(j)))
{printf("%d %d\n",i,j);m++;a[i]++;a[j]+
+;f[find(i)]=find(j);break;}
return 0;
}
Nowadays the one-way traffic is introduced all over the world in order to improve
#include<bits/stdc++.h>
int s[105],e[105];
int main(){
int n,ans=0,res=0;cin>>n;
while(n--){
int a,b,c;cin>>a>>b>>c;
if(s[a]||e[b])res+=c,s[b]=e[a]=1;
else s[a]=e[b]=1;
ans+=c;
cout<<min(res,ans-res);
#include<bits/stdc++.h>
int n,m,v,u;
int main(){
cin>>n>>m>>v;
m-=n-1;
m--;
if(!m)return 0;
We call two numbers x and y similar if they have the same parity
#include<bits/stdc++.h>
int t,n,a[52],s,o,i;
int main(){
for(scanf("%d",&t);t--;){
for(std::sort(a,a+n),s=0;--i;) s|=(a[i]-a[i-1]==1);
puts(o&1&(!s)?"NO":"YES");
One Egyptian boy called Aabid wants to present a string of beads to his friend
#include <bits/stdc++.h>
#define R return
#define I int
#define L long long
L f[55][2][2],K,d;I a[55],n;
rep(i,0,1)rep(j,0,1)if(a[l]-!i&&a[r]-!j&&(l<r||i==j)&&(x||i<=j)&&(y||i<=(!j)))F+=C(l+1,r-1,x||(i<j),y||
(i<!j));R F;
I main(){
cin>>n>>K;c(a)c(f)if(C(1,n,a[1]=0,0)<++K)R cout<<-1,0;
rep(i,2,n){c(f)d=C(1,n,a[i]=0,0);K-=(a[i]=(d<K))*d;}
rep(i,1,n)cout<<a[i];
Students of Winter Informatics School are going to live in a set of houses connected by
underground passages.
#include "bits/stdc++.h"
#define pb push_back
#define mp make_pair
void solve2(){}
int N, M;
int col[MX];
bool visited[MX];
vector<int> adj[MX];
void dfs(int at) {
col[at] = 1;
visited[x] = 1;
dfs(x);
void solve() {
visited[i] = 0;
col[i] = 0;
adj[i].clear();
adj[a].pb(b), adj[b].pb(a);
visited[1] = 1; dfs(1);
return;
vector<int> ans;
int main() {
while (t--) {
solve();
During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen.
#include<bits/stdc++.h>
string s;
int n,t,i;
int main(){
cin>>n>>t>>s;
cout<<s;
The houses are numbered from 1 to N. Underground water pipes connect these houses together.
#include<iostream>
int a[N],w[N],b[N];
int main()
int n,p,x,y,z,i,t,min;
cin>>n>>p;
while(p--)
cin>>x>>y>>z;
a[x]=y;
w[x]=z;
b[x]++;
b[y]+=2;
for(t=0,i=1;i<=n;i++)if(b[i]==1)t++;
printf("%d\n",t);
for(i=1;i<=n;i++)if(b[i]==1)
min=w[i];
t=a[i];
while(a[t]!=0)
if(w[t]<min)min=w[t];
t=a[t];
printf("%d %d %d\n",i,t,min);
return 0;
}
Session 9 : SUM of Subset
#include<bits/stdc++.h>
#define if hha
int main()
int i,t;
cin>>t;
while(t--){
int a[3];
for(i=0;i<3;i++)
cin>>a[i];
sort(a,a+3);
cout<<a[2]+a[1]<<endl;
return 0;
#include <stdio.h>
void solve(){}
int main()
int x,t;
scanf("%d",&t);
while(t--){
scanf("%d",&x);
printf("%d\n",100-x);
return 0;
Tesla recently found a new rectangular electric board that he would like to recycle
#include<bits/stdc++.h>
int A[309][309];
int main() {
int Q,rep;
cin >> Q;
cl = min(cl, A[i][k]);
cr = max(cr, A[i][k]);
if (cr - cl <= K) {
ok[i][j][k] = true;
}
else {
ok[i][j][k] = false;
int ans = 0;
int cont = 0;
if (ok[k][i][j]) ++cont;
else cont = 0;
return 0;
Tamil New Year is approaching and thus Ganesan wants to buy some maha lactos for someone
special.
#include <stdio.h>
void solve(){}
int main()
{
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",a/b);
return 0;
Mani bought N items from a Nilgiris super market. Although it is hard to carry all these items in
hand,
#include<iostream>
void solve(){}
int main()
double n;
cin>>n;
while(n--){
int x;
cin>>x;
//if(x%10==0)
//cout<<x/10<<endl;
// else
// cout<<x/10+1<<endl;