Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solutions #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ABCDEF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//author : codeminion(SPOJ)


#include <bits/stdc++.h>
using namespace std;
int main ()
{
int n;
scanf("%d",&n);
int a[n];
int i,j,k;
for (i=0;i<n;i++)
scanf("%d",&a[i]);
vector <int> v1;
vector <int> v2;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++){
v1.push_back(a[i]*a[j]+a[k]);
}
for (i=0;i<n;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
if (a[i])
v2.push_back(a[i]*(a[j]+a[k]));
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
int s=v1.size();
long long int sum=0;
for (i=0;i<s;i++){
int lo=lower_bound(v2.begin(),v2.end(),v1[i])-v2.begin();
int hi=upper_bound(v2.begin(),v2.end(),v1[i])-v2.begin();
sum+=(hi-lo);
}
cout<<sum<<endl;
return 0;
}
61 changes: 61 additions & 0 deletions ABCPATH.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//author : codeminion(SPOJ)


#include <bits/stdc++.h>
using namespace std;
int main ()
{
int r,c,i,j,x,y,a[50][50],ma,cs=1;
//m1 and m2 store the possiblities of traversal in the 2D grid
int m1[]={-1,0,1,1,1,0,-1,-1}; //store the possible x location traversal
int m2[]={-1,-1,-1,0,1,1,1,0}; //stores the possible y location traversal

char s[50][50]; //input array
bool visited[50][50];

queue < pair<int,int> > q; // queue to hold the coordinates of 2D array for BFS
pair <int,int> p;

while (scanf("%d %d",&r,&c), r!=0, c!=0) {
memset(a,0,sizeof(a));
memset(visited, false , sizeof(visited));

//taking input
for (ma=i=0;i<r;i++)
scanf("%s",&s[i]);

for (i=0;i<r;i++){
for (j=0;j<c;j++){
if (s[i][j]=='A'){
a[i][j]=1;
ma=1;
q.push(make_pair(i,j));
visited[i][j]=true;
}
}
}

// Initializing BFS & continuing till queue is empty (BFS is complete)
while (!q.empty()){
p=q.front();
q.pop();
//checking all 8 possibilities from a position
for (i=0;i<8;i++){
x=m1[i]+p.first;
y=m2[i]+p.second;
//checking if the new coordinate is within bounds and is next
if (x>=0 && x<r && y>=0 && y<c && (s[x][y]==(1+s[p.first][p.second]))){
if (visited[x][y]==false){
a[x][y]=1+a[p.first][p.second];
visited[x][y]=true;
if (a[x][y] > ma)
ma=a[x][y];
q.push(make_pair(x,y));
}
}
}
}
printf("Case %d: %d\n",cs++,ma);
}
return 0;
}
111 changes: 111 additions & 0 deletions BOTTOM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//author : codeminion(SPOJ)


#include <bits/stdc++.h>
using namespace std;
int visited[5010];
int color[5010];
int val[5010];
vector < pair<int,int> > ed;
vector <int> g[5010];
vector <int> gr[5010];
stack <int> st;

void dfs1(int u)
{
int i;
if (visited[u]==1)
return;
visited[u]=1;
for (i=0;i<g[u].size();i++){
int v=g[u][i];
if (visited[v]==0)
dfs1(v);
}
st.push(u);
}


void dfs2(int u,int col)
{
int i;
if (visited[u]==1)
return;
visited[u]=1;
color[u]=col;
for (i=0;i<gr[u].size();i++){
int v=gr[u][i];
if (visited[v]==0)
dfs2(v,col);
}
}

void func()
{
int i;
for (i=0;i<ed.size();i++){
int x=ed[i].first;
int y=ed[i].second;
if (color[x]!=color[y]){
// cout<<"*";
val[color[x]]=0;
}
}
}


int main ()
{
//freopen("input.txt","r",stdin);
while (1){
int v,e,i;
scanf("%d %d",&v,&e);
if (v==0)
break;
memset(visited,0,sizeof(visited));
memset(color,0,sizeof(color));
//memset(yes,0,sizeof(yes));
memset(val,0,sizeof(val));
ed.clear();
for (i=0;i<=v;i++){
g[i].clear();
gr[i].clear();
}
while (!st.empty())
st.pop();

for (i=0;i<e;i++){
int x,y;
scanf("%d %d",&x,&y);
ed.push_back(make_pair(x,y));
g[x].push_back(y);
gr[y].push_back(x);
}

for (i=1;i<=v;i++){
if (visited[i]==0)
dfs1(i);
}

memset(visited,0,sizeof(visited));
int cow=1;


while (!st.empty()){
int po=st.top();
st.pop();
if (visited[po]==0)
dfs2(po,cow);
val[cow]=1;
cow++;
}

func();
for (i=1;i<=v;i++){
if (val[color[i]]==1)
printf("%d ",i);
}
printf("\n");
}
return 0;
}