Question requirements (1) | ai | <T for all I (2) (vi, vj) in E <=> | ai-aj |> = T. Because (1) conditions exist, (2) conditions can be established only when ai and aj are positive and negative. It can be seen that the positive and negative values of an element on a certain road in the figure are positive-> negative-> positive-> negative... Obviously, there is no solution when there is a ring in the figure. Color [I] = 0 indicates that the I node is not stained, 1 indicates that the I node weight is positive, and 2 indicates negative. What if there is no odd ring in the figure? For an edge in the image <u, v>, if color [u] = 1, then obviously a [u]-a [v]> = T, color [u] = 2, that is,-(a [u]-a [v])> = T. If <u, v> is not an edge in the graph, yes | a [u]-a [v] | <T. Two inequalities can also be obtained from the color array. After obtaining the inequality group, you can run the spfa negative ring without any brains... The spfa of the negative ring alone does not need to consider adding 0 nodes. It is enough to add each node to the queue once during initialization. And the d array can be fully initialized to 0. Because you only need to determine the negative ring.
# Include <algorithm> # include <iostream> # include <cstring> # include <cstdlib> # include <fstream> # include <sstream> # include <vector> # include <string> # include <cstdio> # include <bitset> # include <stack> # include <queue> # include <cmath> # include <map> # include <set> # define FF (I, a, B) for (int I = a; I <B; I ++) # define FD (I, a, B) for (int I =; i> = B; I --) # define REP (I, n) for (int I = 0; I <n; I ++) # define CLR (a, B) memset (a, B, sizeo F (a) # define PB push_back # define LL long # define eps 1e-10 # define debug puts ("** debug **") using namespace std; const int maxn = 333; const int T = 1000; struct Edge {int from, to, dist ;}; vector <Edge> edges; vector <int> G [maxn]; vector <int> g [maxn]; int n, ncase, color [maxn], flag, d [maxn], cnt [maxn]; bool inq [maxn]; char ch [maxn] [maxn]; void init () {REP (I, n) G [I]. clear (), g [I]. clear (); edges. clear (); C LR (color, 0);} void add (int a, int B, int c) {edges. PB (Edge) {a, B, c}); int nc = edges. size (); G [a]. PB (nc-1);} void dfs (int u, int c) // binning {color [u] = c; int nc = g [u]. size (); REP (I, nc) {int v = g [u] [I]; if (! Color [v]) dfs (v, 3-c) ;}} bool spfa () {queue <int> q; REP (I, n) d [I] = cnt [I] = 0, inq [I] = 1, q. push (I); while (! Q. empty () {int u = q. front (); q. pop (); inq [u] = false; REP (I, G [u]. size () {Edge & e = edges [G [u] [I]; if (d [e. to]> d [u] + e. dist) {d [e. to] = d [u] + e. dist; if (! Inq [e. to]) {q. push (e. to); inq [e. to] = true; if (++ cnt [e. to]> n) return true ;}}} return false;} bool solve () {// judge the odd circle REP (I, n) if (! Color [I]) dfs (I, 1); REP (I, n) REP (j, g [I]. size () if (color [I] = color [g [I] [j]) return 0; REP (I, n) {FF (j, I + 1, n) {if (ch [I] [j] = '1') {if (color [I] = 1) add (I, j, -T); else add (j, I,-T);} else {if (color [I] = 1) add (j, I, T-1 ); else add (I, j, T-1) ;}} if (spfa () return 0; return 1 ;}int main () {scanf ("% d ", & ncase); while (ncase --) {init (); scanf ("% d", & n); REP (I, n) {scanf ("% s ", ch [I]); REP (j, n) if (I! = J & ch [I] [j] = '1') g [I]. PB (j);} if (solve () puts ("Yes"); else puts ("No");} return 0 ;}