#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=1010000; VI e[N]; int n,m,a,b,cnt,dfn[N],low[N],st[N],bel[N],top,ind; PII sg[N],vs[N]; int way[N]; ll dp[N]; bool ins[N]; void tarjan(int u) { dfn[u]=low[u]=++ind; ins[u]=1; st[++top]=u; rep(i,0,SZ(e[u])) { int v=e[u][i]; if (!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]); else if (ins[v]) low[u]=min(low[u],low[v]); } if (dfn[u]==low[u]) { ++cnt; VI z; while (1) { bel[st[top]]=cnt; z.pb(st[top]); ins[st[top]]=0; if (st[top--]==u) break; } vector<PII> seg; for (auto u:z) { if (u<=a) seg.pb(mp(u,u)); for (auto v:e[u]) { if (bel[v]==bel[u]) continue; if (sg[bel[v]]!=mp(-1,-1)) { auto ss=sg[bel[v]]; if (ss.fi>ss.se) { seg.pb(mp(1,ss.se)); seg.pb(mp(ss.fi,a)); } else { seg.pb(ss); } } } } //for (auto u:z) fprintf(stderr,"%d ",u); fprintf(stderr,":"); sort(all(seg)); //for (auto x:seg) fprintf(stderr," %d,%d",x.fi,x.se); fprintf(stderr,"\n"); if (seg.empty()) sg[bel[u]]=mp(-1,-1); else { vector<PII> sseg; int pval=seg[0].se; int pre=seg[0].fi; for (auto x:seg) { if (x.fi<=pval+1) pval=max(pval,x.se); else { sseg.pb(mp(pre,pval)); pre=x.fi; pval=x.se; } } sseg.pb(mp(pre,pval)); assert(SZ(sseg)<=2); if (SZ(sseg)==1) { sg[bel[u]]=sseg[0]; } else { assert(sseg[0].fi==1); assert(sseg[1].se==a); sg[bel[u]]=mp(sseg[1].fi,sseg[0].se); } } //fprintf(stderr,"faq %d : %d %d\n",u,sg[bel[u]].fi,sg[bel[u]].se); } } int main() { scanf("%d%d%d%d",&n,&m,&a,&b); for (int i=0;i<m;i++) { static char s[5]; int u,v; scanf("%d%s%d",&u,s,&v); if (s[1]=='>') { e[v].pb(u); } else { e[v].pb(u); e[u].pb(v); } } for (int i=1;i<=n;i++) if (!dfn[i]) { tarjan(i); } ll ans=1; int emp=0,ful=0,cmid=0; map<PII,int> hs; int w=0; int mingap=n+1; int off=0; VI ps; for (int i=a+1;i<=a+b;i++) { auto x=sg[bel[i]]; //fprintf(stderr,"%d %d\n",x.fi,x.se); if (x==mp(-1,-1)) emp++; else if (x==mp(1,a)) ful++; else { cmid++; vs[w++]=x; ps.pb(x.fi-1); ps.pb(x.se); } } ps.pb(0); ps.pb(a); sort(all(ps)); ps.erase(unique(all(ps)),ps.end()); a=SZ(ps)-1; for (int i=0;i<w;i++) { auto x=vs[i]; vs[i].fi=lower_bound(all(ps),x.fi-1)-ps.begin(); vs[i].se=lower_bound(all(ps),x.se)-ps.begin(); x=vs[i]; int gap=x.se-x.fi; if (gap<0) gap+=a; if (gap<mingap) { mingap=gap; off=(a-x.fi)%a; } } rep(i,0,w) { PII &x=vs[i]; x.fi=(x.fi+off)%a,x.se=(x.se+off)%a; if (x.se<x.fi) x.se+=a; hs[x]++; } w=0; ans=0; for (auto x:hs) { w++; vs[w]=x.fi; way[w]=(powmod(2,x.se)-1)%mod; //printf("zz %d %d %d\n",w,vs[w].fi,vs[w].se); } for (int fa=0;fa<=vs[1].se;fa++) { vs[0].se=fa; dp[0]=1; ll sm=1; int r=0; for (int i=1;i<=w;i++) { while (vs[r].se<vs[i].fi) { sm=sm-dp[r]; if (sm<0) sm+=mod; r++; } dp[i]=sm*way[i]%mod; sm=(sm+dp[i])%mod; if (vs[i].se==fa+a) ans=(ans+dp[i])%mod; // printf("%d %d %lld\n",fa,i,dp[i]); } } ans=(ans+powmod(2,ful+cmid)-powmod(2,cmid))%mod; ans=ans*powmod(2,emp)%mod; if (ans<0) ans+=mod; printf("%lld\n",ans); }
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 174 175 176 177 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=1010000; VI e[N]; int n,m,a,b,cnt,dfn[N],low[N],st[N],bel[N],top,ind; PII sg[N],vs[N]; int way[N]; ll dp[N]; bool ins[N]; void tarjan(int u) { dfn[u]=low[u]=++ind; ins[u]=1; st[++top]=u; rep(i,0,SZ(e[u])) { int v=e[u][i]; if (!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]); else if (ins[v]) low[u]=min(low[u],low[v]); } if (dfn[u]==low[u]) { ++cnt; VI z; while (1) { bel[st[top]]=cnt; z.pb(st[top]); ins[st[top]]=0; if (st[top--]==u) break; } vector<PII> seg; for (auto u:z) { if (u<=a) seg.pb(mp(u,u)); for (auto v:e[u]) { if (bel[v]==bel[u]) continue; if (sg[bel[v]]!=mp(-1,-1)) { auto ss=sg[bel[v]]; if (ss.fi>ss.se) { seg.pb(mp(1,ss.se)); seg.pb(mp(ss.fi,a)); } else { seg.pb(ss); } } } } //for (auto u:z) fprintf(stderr,"%d ",u); fprintf(stderr,":"); sort(all(seg)); //for (auto x:seg) fprintf(stderr," %d,%d",x.fi,x.se); fprintf(stderr,"\n"); if (seg.empty()) sg[bel[u]]=mp(-1,-1); else { vector<PII> sseg; int pval=seg[0].se; int pre=seg[0].fi; for (auto x:seg) { if (x.fi<=pval+1) pval=max(pval,x.se); else { sseg.pb(mp(pre,pval)); pre=x.fi; pval=x.se; } } sseg.pb(mp(pre,pval)); assert(SZ(sseg)<=2); if (SZ(sseg)==1) { sg[bel[u]]=sseg[0]; } else { assert(sseg[0].fi==1); assert(sseg[1].se==a); sg[bel[u]]=mp(sseg[1].fi,sseg[0].se); } } //fprintf(stderr,"faq %d : %d %d\n",u,sg[bel[u]].fi,sg[bel[u]].se); } } int main() { scanf("%d%d%d%d",&n,&m,&a,&b); for (int i=0;i<m;i++) { static char s[5]; int u,v; scanf("%d%s%d",&u,s,&v); if (s[1]=='>') { e[v].pb(u); } else { e[v].pb(u); e[u].pb(v); } } for (int i=1;i<=n;i++) if (!dfn[i]) { tarjan(i); } ll ans=1; int emp=0,ful=0,cmid=0; map<PII,int> hs; int w=0; int mingap=n+1; int off=0; VI ps; for (int i=a+1;i<=a+b;i++) { auto x=sg[bel[i]]; //fprintf(stderr,"%d %d\n",x.fi,x.se); if (x==mp(-1,-1)) emp++; else if (x==mp(1,a)) ful++; else { cmid++; vs[w++]=x; ps.pb(x.fi-1); ps.pb(x.se); } } ps.pb(0); ps.pb(a); sort(all(ps)); ps.erase(unique(all(ps)),ps.end()); a=SZ(ps)-1; for (int i=0;i<w;i++) { auto x=vs[i]; vs[i].fi=lower_bound(all(ps),x.fi-1)-ps.begin(); vs[i].se=lower_bound(all(ps),x.se)-ps.begin(); x=vs[i]; int gap=x.se-x.fi; if (gap<0) gap+=a; if (gap<mingap) { mingap=gap; off=(a-x.fi)%a; } } rep(i,0,w) { PII &x=vs[i]; x.fi=(x.fi+off)%a,x.se=(x.se+off)%a; if (x.se<x.fi) x.se+=a; hs[x]++; } w=0; ans=0; for (auto x:hs) { w++; vs[w]=x.fi; way[w]=(powmod(2,x.se)-1)%mod; //printf("zz %d %d %d\n",w,vs[w].fi,vs[w].se); } for (int fa=0;fa<=vs[1].se;fa++) { vs[0].se=fa; dp[0]=1; ll sm=1; int r=0; for (int i=1;i<=w;i++) { while (vs[r].se<vs[i].fi) { sm=sm-dp[r]; if (sm<0) sm+=mod; r++; } dp[i]=sm*way[i]%mod; sm=(sm+dp[i])%mod; if (vs[i].se==fa+a) ans=(ans+dp[i])%mod; // printf("%d %d %lld\n",fa,i,dp[i]); } } ans=(ans+powmod(2,ful+cmid)-powmod(2,cmid))%mod; ans=ans*powmod(2,emp)%mod; if (ans<0) ans+=mod; printf("%lld\n",ans); } |