-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1684-Giant Pizza.cpp
138 lines (110 loc) · 2.42 KB
/
1684-Giant Pizza.cpp
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
// JAI BAJARANG BALI
// manitianajay45
// give me some sunshine, give me some rain , give me another chance to grow up once again....
// sab moh maya hai....
/*
2SAT problem
https://cses.fi/book/book.pdf
page no - 160-162
/*
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
ll n,m;
bool visited[200005][2];
string ans(200005,'*');
vector<pair<ll,ll>> graph[200005][2];
vector<pair<ll,ll>> order;
vector<pair<ll,ll>> graphr[200005][2];
ll vis[200005][2];
ll an=0;
void topo(ll v,ll tp){
visited[v][tp]=true;
for(auto u:graph[v][tp]){
if(!visited[u.first][u.second]){
topo(u.first,u.second);
}
}
order.push_back({v,tp});
}
bool flag=true;
void dfs(ll v,ll tp){
if(tp==0 && vis[v][1]==an){
flag=false;
}
if(tp==1 && vis[v][0]==an){
flag=false;
}
vis[v][tp]=an;
if(ans[v]=='*'){
if(tp==0){
ans[v]='+';
}else{
ans[v]='-';
}
}
for(auto u:graphr[v][tp]){
if(vis[u.first][u.second]==0){
dfs(u.first,u.second);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll n;
cin >> n>>m;
for (ll i = 0; i <n; i++)
{
ll u,v;
char ui,vi;
cin>>ui>>u>>vi>>v;
ll fui,fvi;
fui=(ui=='+')?1:0;
fvi=(vi=='+')?1:0;
if(fui==0){
graph[u][1].push_back({v,fvi});
graphr[v][fvi].push_back({u,1});
}else{
graph[u][0].push_back({v,fvi});
graphr[v][fvi].push_back({u,0});
}
if(fvi==0){
graph[v][1].push_back({u,fui});
graphr[u][fui].push_back({v,1});
}else{
graph[v][0].push_back({u,fui});
graphr[u][fui].push_back({v,0});
}
}
for(ll i=1;i<=m;i++){
if(!visited[i][0]){
topo(i,0);
}
if(!visited[i][1]){
topo(i,1);
}
}
reverse(order.begin(),order.end());
for(auto i:order){
if(vis[i.first][i.second]==0){
an++;
dfs(i.first,i.second);
if(!flag){
break;
}
// an++;
}
}
if(!flag){
cout<<"IMPOSSIBLE"<<endl;
return 0;
}
for(ll i=1;i<=m;i++){
cout<<ans[i];
}
cout<<endl;
return 0;
}