-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAKBAR.c
178 lines (159 loc) · 3.79 KB
/
AKBAR.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef long long ll;
typedef struct queue{
int front;
int rear;
int items[1000000];
}queue ;
int isEmpty(queue* q) {
if(q->rear == -1)
return 1;
else
return 0;
}
void enqueue(queue *q,int value){
if(q->front == -1)
q->front = 0;
q->items[++q->rear] = value;
}
int dequeue(queue *q){
int item = -1;
if(isEmpty(q)==0){
item = q->items[q->front];
q->front++;
if(q->front > q->rear)
q->front = q->rear = -1;
}
return item;
}
queue* createQueue() {
queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
typedef struct node
{
ll dest;
struct node *next;
}node;
struct list
{
struct node *start;
};
struct Graph
{
ll v;
struct list *a;
};
struct Graph *create_graph(ll vertices)
{
if (vertices == 0)
{
return NULL;
}
struct Graph *g = (struct Graph *)malloc(sizeof(struct Graph));
g->v = vertices;
g->a = (struct list *)malloc((vertices) * sizeof(struct list));
ll i;
for (i = 0; i < vertices; i++)
{
g->a[i].start = NULL;
}
return g;
}
void addedge(struct Graph *g, ll source, ll destination)
{
// Add an edge from src to dest
struct node *ptr1 = (struct node *)malloc(sizeof(struct node));
ptr1->dest = destination;
ptr1->next = g->a[source].start;
g->a[source].start = ptr1;
// Since graph is undirected, add an edge from
// dest to src also
struct node *ptr2 = (struct node *)malloc(sizeof(struct node));
ptr2->dest = source;
ptr2->next = g->a[destination].start;
g->a[destination].start = ptr2;
}
int bfs(struct Graph* graph,int soldier[],int n)
{
char count[n+1];
memset(count, 0, sizeof(count));
// memset(dist, 0, sizeof(dist));
for (int i = 1; i < n+1; i++)
{
if(soldier[i] == -1) continue;
if(count[i]) {
// printf("%d frf\n", i);
return 0;
}
count[i] = 1;
int q[2*n], f = 0, r = 0;
int dist[n+1];
char vis[n+1];
memset(vis, 0, sizeof(vis));
dist[i] = 0;
q[r++] = i;
vis[i] = 1;
while(f < r) {
int curr = q[f++];
node* nb = graph->a[curr].start;
if(dist[curr] == soldier[i]) continue;
while(nb) {
if(vis[nb->dest]) {
nb = nb->next; continue;
}
if(count[nb->dest]){
// printf("%d %d %d fff\n", i, curr, nb->dest);
return 0;
}
count[nb->dest] = 1;
dist[nb->dest] = dist[curr] + 1;
q[r++] = nb->dest;
vis[nb->dest] = 1;
nb = nb->next;
}
}
}
for (int i = 1; i < n+1; i++)
{
if(count[i] != 1) return 0;
}
return 1;
}
int main(){
int test;
scanf("%d",&test);
while(test--){
int yes=1;
int N,R,M; //N:no. of cities, R:paths , M:soldiers
scanf("%d %d %d",&N,&R,&M);
struct Graph* graph=create_graph(N+1);
for(int i=0;i<R;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(graph,a,b);
}
int soldier[N+1];
// for(int i=0;i<M;i++){
// int k,s;
// scanf("%d %d",&k,&s);
// k--;
// }
memset(soldier, -1, sizeof(soldier));
for (int i = 0; i < M; i++)
{
int x, s; scanf("%d %d",&x,&s);
if(soldier[x] != -1) {
yes = 0; break;
}
soldier[x] = s;
}
if(yes && bfs(graph, soldier, N)) printf("Yes\n");
else printf("No\n");
}
}