Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77f6dc2

Browse files
Added more stuff
1 parent f77fca7 commit 77f6dc2

File tree

6 files changed

+406
-4
lines changed

6 files changed

+406
-4
lines changed

‎Advanced Graphs/Connectedhorses.cpp‎

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
/*
8+
You all must be familiar with the chess-board having
9+
8*8 squares of alternate black and white cells. Well, here
10+
we have for you a similar
11+
N*M size board with similar arrangement of black and white
12+
cells.
13+
A few of these cells have Horses placed over them.
14+
Each horse is unique. Now these horses are not the
15+
usual horses which could jump to any of the
16+
8 positions they usually jump in. They can move only if there is another horse on one of the 8-positions that it can go to usually and then both the horses will swap their positions. This swapping can happen infinitely times.
17+
A photographer was assigned to take a picture of all the different ways that the horses occupy the board! Given the state of the board, calculate answer. Sincethis answer may be quite large, calculate in modulo
18+
10^9+7
19+
Input:
20+
First line contains
21+
T which is the number of test cases.
22+
T test cases follow first line of each containing three integers
23+
N, M and Q where
24+
N,M is the size of the board and
25+
Q is the number of horses on it.
26+
27+
Q lines follow each containing the 2 integers
28+
X and Y which are the coordinates of the Horses.
29+
Output:
30+
For each test case, output the number of photographs taken by photographer.
31+
Constraints:
32+
1<=T<=10
33+
1<=N,M<=1000
34+
1<=Q<=N*M
35+
SAMPLE INPUT
36+
2
37+
4 4 4
38+
1 1
39+
1 2
40+
3 1
41+
3 2
42+
4 4 4
43+
1 1
44+
1 2
45+
3 1
46+
4 4
47+
SAMPLE OUTPUT
48+
4
49+
2
50+
*/
51+
52+
53+
#include <bits/stdc++.h>
54+
55+
using namespace std;
56+
#define pii pair<ll,ll>
57+
#define MOD 1000000007
58+
#define ll long long
59+
60+
ll dfs(ll i, ll j, vector<pii>** graph, ll** visited){
61+
visited[i][j] = 1;
62+
ll answer = 1;
63+
64+
for(ll k = 0; k < graph[i][j].size(); k++){
65+
ll x = graph[i][j][k].first;
66+
ll y = graph[i][j][k].second;
67+
if(!visited[x][y]){
68+
answer = (answer+dfs(x, y, graph, visited))%MOD;
69+
}
70+
}
71+
72+
return answer;
73+
74+
// ll count = 0;
75+
// for (ll i = 0; i < graph[sx][sy].size(); ++i)
76+
// {
77+
// ll left = (graph[sx][sy].at(i)).first;
78+
// ll right = (graph[sx][sy].at(i)).second;
79+
// if (visited[left][right] == 0)
80+
// {
81+
// visited[left][right] = 1;
82+
// count = (count+1)%MOD;
83+
// }
84+
85+
// }
86+
87+
// return count%MOD;
88+
}
89+
90+
void fill_vector(vector<pii>** graph, ll** board, ll m, ll n){
91+
for (ll i = 0; i < n; ++i)
92+
{
93+
for (ll j = 0; j < m; ++j)
94+
{
95+
if (board[i][j] == 1)
96+
{
97+
graph[i][j].push_back(make_pair(i, j));
98+
99+
if (i+2<n && j+1<m && board[i+2][j+1] == 1)
100+
{
101+
//return ;
102+
graph[i][j].push_back(make_pair(i+2, j+1));
103+
}
104+
//return ;
105+
106+
107+
if (i+2<n && j-1>=0 && board[i+2][j-1] == 1)
108+
{
109+
graph[i][j].push_back(make_pair(i+2, j-1));
110+
}
111+
112+
if (i-2>=0 && j+1<m && board[i-2][j+1] == 1)
113+
{
114+
graph[i][j].push_back(make_pair(i-2, j+1));
115+
}
116+
117+
if (i-2>=0 && j-1>=0 && board[i-2][j-1] == 1)
118+
{
119+
graph[i][j].push_back(make_pair(i-2, j-1));
120+
}
121+
122+
if (i+1<n && j+2<m && board[i+1][j+2] == 1)
123+
{
124+
graph[i][j].push_back(make_pair(i+1, j+2));
125+
}
126+
127+
if (i+1<n && j-2>=0 && board[i+1][j-2] == 1)
128+
{
129+
graph[i][j].push_back(make_pair(i+1, j-2));
130+
}
131+
132+
if (i-1>=0 && j+2<m && board[i-1][j+2] == 1)
133+
{
134+
graph[i][j].push_back(make_pair(i-1, j+2));
135+
}
136+
137+
if (i-1>=0 && j-2>=0 && board[i-1][j-2] == 1)
138+
{
139+
graph[i][j].push_back(make_pair(i-1, j-2));
140+
}
141+
}
142+
}
143+
}
144+
145+
return;
146+
}
147+
148+
149+
int main( int argc , char ** argv )
150+
{
151+
152+
ios_base::sync_with_stdio(false) ;
153+
cin.tie(NULL) ;
154+
155+
//To get the factorial
156+
ll *factorial = new ll[1000000];
157+
factorial[1] = 1;
158+
for(ll i = 2; i < 1000000; i++){
159+
factorial[i] = (factorial[i-1]*i)%MOD;
160+
}
161+
162+
//Main part of the input
163+
ll t;
164+
cin>>t;
165+
while(t--){
166+
ll n, m;
167+
ll q;
168+
cin>>n>>m>>q;
169+
170+
//Create a 2d array for board
171+
ll** board = new ll*[n];
172+
for (ll i = 0; i < n; ++i)
173+
{
174+
board[i] = new ll[m];
175+
for (ll j = 0; j < m; ++j)
176+
{
177+
board[i][j] = 0;
178+
}
179+
}
180+
181+
//Fill the board
182+
while(q--){
183+
ll x, y;
184+
cin>>x>>y;
185+
board[x-1][y-1] = 1;
186+
}
187+
188+
189+
//Creating a graph 2d array
190+
vector<pii>** graph = new vector<pii>*[(n)];
191+
for (ll i = 0; i < n; ++i)
192+
{
193+
graph[i] = new vector<pii>[m];
194+
}
195+
196+
//Fill the graph
197+
fill_vector(graph, board, m, n);
198+
199+
// for (ll i = 0; i < n; ++i)
200+
// {
201+
// for (ll j = 0; j < m; ++j)
202+
// {
203+
// if (graph[i][j].size()!=0)
204+
// {
205+
// for (ll k = 0; k < graph[i][j].size(); ++k)
206+
// {
207+
// cout<<graph[i][j][k].first<<" "<<graph[i][j][k].second<<" ";
208+
// }
209+
// cout <<'\n';
210+
// }
211+
// }
212+
// }
213+
214+
//Count and multiply the number of elements
215+
ll** visited = new ll*[n];
216+
for (ll i = 0; i < n; ++i)
217+
{
218+
visited[i] = new ll[m];;
219+
for (ll j = 0; j < m; ++j)
220+
{
221+
visited[i][j] = 0;
222+
}
223+
}
224+
225+
ll ans = 1;
226+
for (ll i = 0; i < n; ++i)
227+
{
228+
for (ll j = 0; j < m; ++j)
229+
{
230+
if (visited[i][j] == 0 && board[i][j] == 1)
231+
{
232+
// cout << i<<" "<<j<<" "<<" "<<dfs(i, j, graph, visited, m, n) << '\n';
233+
// cout << "Here "<<factorial[2] << '\n';
234+
// cout << i<<" "<<j<<" "<<" "<<dfs(i, j, graph, visited, m, n) << '\n';
235+
236+
ans = (ans*factorial[dfs(i, j, graph, visited)])%MOD;
237+
}
238+
239+
}
240+
}
241+
242+
cout << ans << '\n';
243+
}
244+
245+
246+
return 0 ;
247+
248+
}

‎Advanced Graphs/Permutationsswap.cpp‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
Kevin has a permutation P of N integers 1, 2, ..., N, but he doesn't like it. Kevin wants to get a permutation Q.
10+
11+
Also he believes that there are M good pairs of integers (ai , bi). Kevin can perform following operation with his permutation:
12+
13+
Swap Px and Py only if (x, y) is a good pair.
14+
Help him and tell if Kevin can obtain permutation Q using such operations.
15+
Input format:
16+
The first line of input will contain an integer T, denoting the number of test cases.
17+
18+
Each test case starts with two space-separated integers N and M. The next line contains N space-separated integers Pi. The next line contains N space-separated integers Qi. Each of the next M lines contains two space-separated integers ai and bi.
19+
Output format:
20+
For every test case output "YES" (without quotes) if Kevin can obtain permutation Q and "NO" otherwise.
21+
Constraints:
22+
1 ≤ T ≤ 10
23+
2 ≤ N ≤ 100000
24+
1 ≤ M ≤ 100000
25+
1 ≤ Pi, Qi ≤ N. Pi and Qi are all distinct.
26+
1 ≤ ai < bi ≤ N
27+
SAMPLE INPUT
28+
2
29+
4 1
30+
1 3 2 4
31+
1 4 2 3
32+
3 4
33+
4 1
34+
1 3 2 4
35+
1 4 2 3
36+
2 4
37+
SAMPLE OUTPUT
38+
NO
39+
YES
40+
*/
41+
42+
43+
#include <bits/stdc++.h>
44+
using namespace std;
45+
46+
void dfs(vector<int> *graph, int start, bool *isVisited, unordered_set<int> *part){
47+
isVisited[start] = true;
48+
part->insert(start);
49+
50+
for(int i = 0; i < graph[start].size(); i++){
51+
int next = graph[start][i];
52+
if(!isVisited[next]){
53+
dfs(graph, next, isVisited, part);
54+
}
55+
}
56+
}
57+
58+
unordered_set< unordered_set<int> *> *getComponent(vector<int> *graph, int n){
59+
bool *isVisited = new bool[n];
60+
for(int i = 0; i < n; i++) isVisited[i] = false;
61+
62+
unordered_set< unordered_set<int> * > *component = new unordered_set< unordered_set<int> * >();
63+
for(int i = 0; i < n; i++){
64+
if(!isVisited[i]){
65+
unordered_set<int> *part = new unordered_set<int>();
66+
dfs(graph, i, isVisited, part);
67+
component->insert(part);
68+
}
69+
}
70+
71+
return component;
72+
}
73+
74+
int main(){
75+
int flag;
76+
int t;
77+
cin >> t;
78+
while(t--){
79+
flag = 1;
80+
int n, m;
81+
cin >> n >> m;
82+
83+
int *p = new int[n];
84+
for(int i = 0; i < n; i++){
85+
int x;
86+
cin >> x;
87+
p[i] = x-1;
88+
}
89+
90+
int *q = new int[n+1];
91+
for(int i = 0; i < n; i++){
92+
int x;
93+
cin >> x;
94+
q[i] = x-1;
95+
}
96+
97+
vector<int> *graph = new vector<int>[n];
98+
for(int i = 0; i < m; i++){
99+
int a, b;
100+
cin >> a >> b;
101+
graph[a-1].push_back(b-1);
102+
graph[b-1].push_back(a-1);
103+
}
104+
105+
unordered_set< unordered_set<int> * > *component = getComponent(graph, n);
106+
107+
// for(unordered_set< unordered_set<int> * >::iterator i = component->begin(); i != component->end(); i++){
108+
// for(unordered_set<int>::iterator j = (*i)->begin(); j != (*i)->end(); j++){
109+
// cout << (*j)+1 << " ";
110+
// }
111+
// cout << endl;
112+
// }
113+
114+
for(unordered_set< unordered_set<int> * >::iterator i = component->begin(); i != component->end(); i++){
115+
set<int> setA;
116+
set<int> setB;
117+
for(unordered_set<int>::iterator j = (*i)->begin(); j != (*i)->end(); j++){
118+
setA.insert(p[*j]);
119+
setB.insert(q[*j]);
120+
}
121+
122+
if(setA != setB){
123+
flag = 0;
124+
cout << "NO" << endl;
125+
break;
126+
}
127+
}
128+
129+
if(flag) cout << "YES" << endl;
130+
}
131+
}

‎Advanced Graphs/Test.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1
2+
2 7 12
3+
1 2
4+
2 2
5+
2 6
6+
1 7
7+
1 1
8+
2 3
9+
1 3
10+
1 4
11+
2 1
12+
2 5
13+
2 4
14+
1 6

‎Advanced Graphs/a.out‎

33.3 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /