# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
320158 |
2020-11-07T19:14:53 Z |
ant101 |
Usmjeri (COCI17_usmjeri) |
C++14 |
|
1226 ms |
222600 KB |
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
#define mp make_pair
#define f first
#define s second
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 3e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) {
setIn(s+".in");
setOut(s+".out");
} // for USACO
}
}
using namespace io;
namespace input {
template<class T> void re(complex<T>& x);
template<class T1, class T2> void re(pair<T1,T2>& p);
template<class T> void re(vector<T>& a);
template<class T, size_t SZ> void re(array<T,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(double& x) { string t; re(t); x = stod(t); }
void re(ld& x) { string t; re(t); x = stold(t); }
template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
re(first); re(rest...);
}
template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
namespace output {
template<class T1, class T2> void pr(const pair<T1,T2>& x);
template<class T, size_t SZ> void pr(const array<T,SZ>& x);
template<class T> void pr(const vector<T>& x);
template<class T> void pr(const set<T>& x);
template<class T1, class T2> void pr(const map<T1,T2>& x);
template<class T> void pr(const T& x) { cout << x; }
template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
pr(first); pr(rest...);
}
template<class T1, class T2> void pr(const pair<T1,T2>& x) {
pr("{",x.f,", ",x.s,"}");
}
template<class T> void prContain(const T& x) {
pr("{");
bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
pr("}");
}
template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
template<class T> void pr(const vector<T>& x) { prContain(x); }
template<class T> void pr(const set<T>& x) { prContain(x); }
template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
void ps() { pr("\n"); }
template<class Arg> void ps(const Arg& first) {
pr(first); ps(); // no space at end of line
}
template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
pr(first," "); ps(rest...); // print w/ spaces
}
}
using namespace output;
using namespace input;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
ll N, M;
ll l;
vector<ll> adj[MX];
ll timer;
vector<ll> tin, tout;
vector<vector<ll>> up;
vpl lcapz[MX];
ll postorder[MX];
ll curr = 0;
ll parent[MX];
ll mustequal[MX];
ll pref[MX];
set<pl> childsets[MX];
ll currpref[MX];
ll t = 0;
ll starttime[MX];
ll dp[MX][2];
void dfs(ll v, ll p)
{
tin[v] = ++timer;
up[v][0] = p;
for (ll i = 1; i <= l; ++i)
up[v][i] = up[up[v][i-1]][i-1];
for (ll u : adj[v]) {
if (u != p)
dfs(u, v);
}
tout[v] = ++timer;
}
bool is_ancestor(ll u, ll v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
ll lca(ll u, ll v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (ll i = l; i >= 0; --i) {
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
void preprocess(ll root) {
tin.resize(N);
tout.resize(N);
timer = 0;
l = ceil(log2(N));
up.assign(N, vector<ll>(l + 1));
dfs(root, root);
}
void precdfs(ll u, ll p) {
starttime[u] = t++;
parent[u] = p;
// ps("indfs", u);
trav (v, adj[u]) {
if (v != p) {
childsets[u].insert({t, v});
precdfs(v, u);
}
}
// ps("children", u);
// trav(a, childsets[u]) {
// ps(a);
// }
// ps();
postorder[curr++] = u;
}
map<ll, pl> combs;
map<ll, ll> vis;
vl auxadj[MX];
pl color(ll u, ll p, ll order) {
pl ret = combs[u];
vis[u] = order;
if (order) {
swap(ret.f, ret.s);
}
trav (v, auxadj[u]) {
if (v != p) {
if (vis.count(v)) {
if (vis[v] == vis[u]) {
return {0, 0};
} else {
continue;
}
}
pl val = color(v, u, 1-order);
ret = {mul(ret.f, val.f), mul(ret.s, val.s)};
}
}
return ret;
}
ll f(ll u, ll ud, ll p) {
if (dp[u][ud] != -1) {
return dp[u][ud];
}
dp[u][ud] = 1;
map<ll, pl> ccombs;
trav (v, adj[u]) {
if (v != p) {
if (mustequal[v] && p != -1) {
if (ud) {
ccombs[v] = {0, f(v, 1, u)};
} else {
ccombs[v] = {f(v, 0, u), 0};
}
} else {
ccombs[v] = {f(v, 0, u), f(v, 1, u)};
}
}
}
combs.clear();
vis.clear();
combs = ccombs;
trav(c, lcapz[u]) {
set<pl>::iterator p1 = childsets[u].upper_bound({starttime[c.f], 1e13});
set<pl>::iterator p2 = childsets[u].upper_bound({starttime[c.s], 1e13});
// ps(starttime[c.f], starttime[c.s], *p1, *p2);
ll a, b;
if (p1 == childsets[u].begin() || p2 == childsets[u].begin()) {
continue;
}
--p1;
--p2;
a = (*p1).s;
b = (*p2).s;
auxadj[a].pb(b);
auxadj[b].pb(a);
}
trav (v, adj[u]) {
if (v != p && !vis.count(v)) {
pl val = color(v, u, 0);
dp[u][ud] = mul(dp[u][ud], add(val.f, val.s));
}
}
trav(c, lcapz[u]) {
set<pl>::iterator p1 = childsets[u].upper_bound({starttime[c.f], 1e13});
set<pl>::iterator p2 = childsets[u].upper_bound({starttime[c.s], 1e13});
ll a, b;
if (p1 == childsets[u].begin() || p2 == childsets[u].begin()) {
continue;
}
--p1;
--p2;
a = (*p1).s;
b = (*p2).s;
auxadj[a].clear();
auxadj[b].clear();
}
// ps(u, ud, dp[u][ud]);
return dp[u][ud];
}
int main() {
// setIn("usmjeri.in.3a");
memset(dp, -1, sizeof(dp));
re(N, M);
F0R (i, N-1) {
ll a, b;
re(a, b);
a--;
b--;
adj[a].pb(b);
adj[b].pb(a);
}
preprocess(0);
F0R (i, M) {
ll a, b;
re(a, b);
a--;
b--;
ll node = lca(a, b);
lcapz[node].pb({a, b});
// ps(node, a, b);
}
precdfs(0, -1);
F0R (i, N) {
trav(a, lcapz[i]) {
pref[a.f]++;
pref[a.s]++;
set<pl>::iterator p1 = childsets[i].upper_bound({a.f, 1e13});
set<pl>::iterator p2 = childsets[i].upper_bound({a.s, 1e13});
ll b, c;
if (p1 == childsets[i].begin()) {
b = a.f;
} else {
--p1;
b = (*p1).s;
}
if (p2 == childsets[i].begin()) {
c = a.s;
} else {
--p2;
c = (*p2).s;
}
pref[b]--;
pref[c]--;
}
// ps("pref", i, pref[i]);
}
F0R (i, curr) {
ll v = postorder[i];
// ps(v);
currpref[v]+=pref[v];
currpref[parent[v]]+=currpref[v];
mustequal[v] = (currpref[v] != 0);
// if (mustequal[v]) {
// ps("set", v);
// }
}
ps(f(0, 1, -1));
return 0;
}
Compilation message
usmjeri.cpp: In function 'void io::setIn(std::string)':
usmjeri.cpp:67:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
usmjeri.cpp: In function 'void io::setOut(std::string)':
usmjeri.cpp:68:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
68 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
326 ms |
102884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
806 ms |
222600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
40684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
40552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
42084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
42092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1050 ms |
158424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1226 ms |
161628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1121 ms |
162412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1106 ms |
163240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |