Problem: Problem F Pizza Delivery

Alyssa is a college student, living in New Tsukuba City. All the streets in the city are one-way. A new social experiment starting tomorrow is on alternative traffic regulation reversing the one-way directions of street sections. Reversals will be on one single street section between two adjacent intersections for each day; the directions of all the other sections will not change, and the reversal will be canceled on the next day.

Alyssa orders a piece of pizza everyday from the same pizzeria. The pizza is delivered along the shortest route from the intersection with the pizzeria to the intersection with Alyssa's house.

Altering the traffic regulation may change the shortest route. Please tell Alyssa how the social experiment will affect the pizza delivery route.

Input

The input consists of a single test case in the following format.


$n$ $m$
$a_1$ $b_1$ $c_1$
...
$a_m$ $b_m$ $c_m$


The first line contains two integers, $n$, the number of intersections, and $m$, the number of street sections in New Tsukuba City ($2 \leq n \leq 100 000, 1 \leq m \leq 100 000$). The intersections are numbered $1$ through $n$ and the street sections are numbered $1$ through $m$.

The following $m$ lines contain the information about the street sections, each with three integers $a_i$, $b_i$, and $c_i$ ($1 \leq a_i n, 1 \leq b_i \leq n, a_i \ne b_i, 1 \leq c_i \leq 100 000$). They mean that the street section numbered $i$ connects two intersections with the one-way direction from $a_i$ to $b_i$, which will be reversed on the $i$-th day. The street section has the length of $c_i$. Note that there may be more than one street section connecting the same pair of intersections.

The pizzeria is on the intersection 1 and Alyssa's house is on the intersection 2. It is guaranteed that at least one route exists from the pizzeria to Alyssa's before the social experiment starts.

Output

The output should contain $m$ lines. The $i$-th line should be

* HAPPY if the shortest route on the $i$-th day will become shorter,
* SOSO if the length of the shortest route on the $i$-th day will not change, and
* SAD if the shortest route on the $i$-th day will be longer or if there will be no route from the pizzeria to Alyssa's house.



Alyssa doesn't mind whether the delivery bike can go back to the pizzeria or not.

Sample Input 1


4 5
1 3 5
3 4 6
4 2 7
2 1 18
2 3 12


Sample Output 1


SAD
SAD
SAD
SOSO
HAPPY


Sample Input 2


7 5
1 3 2
1 6 3
4 2 4
6 2 5
7 5 6


Sample Output 2


SOSO
SAD
SOSO
SAD
SOSO


Sample Input 3


10 14
1 7 9
1 8 3
2 8 4
2 6 11
3 7 8
3 4 4
3 2 1
3 2 7
4 8 4
5 6 11
5 8 12
6 10 6
7 10 8
8 3 6


Sample Output 3


SOSO
SAD
HAPPY
SOSO
SOSO
SOSO
SAD
SOSO
SOSO
SOSO
SOSO
SOSO
SOSO
SAD






Example

Input

4 5
1 3 5
3 4 6
4 2 7
2 1 18
2 3 12


Output

SAD
SAD
SAD
SOSO
HAPPY

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxn = 1e5 + 10;
const ll inf = 1e15;

vector< pair< pair< ll , ll> , int > > adj[mxn] , radj[mxn] , dac[mxn] ;
vector< pair< pair<ll , ll >  , ll > > edge;
map< pair< pair< ll , ll > , ll> , int > mp;
ll dis[mxn] , rdis[mxn] , spath[mxn] , rpath[mxn] , low[mxn];
bool eflag[mxn] , nflag[mxn];
void dks(int src , int des)
{
    priority_queue< pair<ll , ll > > PQ;

    dis[src] = 0;
    PQ.push(make_pair(0 , src));
    while(!PQ.empty())
    {
        pair<ll , ll > p = PQ.top();
        PQ.pop();
        for(int i = 0; i < adj[p.second].size(); i++)
        {
            pair<ll , ll > q = adj[p.second][i].first;
            if(dis[q.second] > q.first  + dis[p.second])
            {
                dis[q.second] = dis[p.second] + q.first;
                PQ.push(make_pair((-1)*dis[q.second] , q.second));
            }
        }
    }
}



void rdks(int src , int des)
{
    priority_queue< pair<ll , ll > > PQ;
    rdis[src] = 0;
    PQ.push(make_pair(0 , src));
    while(!PQ.empty())
    {
        pair<ll , ll > p = PQ.top();
        PQ.pop();
        for(int i = 0; i < radj[p.second].size(); i++)
        {
            pair<ll , ll > q = radj[p.second][i].first;
            if(rdis[q.second] > q.first  + rdis[p.second])
            {
                rdis[q.second] = rdis[p.second] + q.first;
                PQ.push(make_pair((-1)*rdis[q.second] , q.second));
            }
        }
    }
}
int cnt = 0;
ll dv[mxn];
void dfs(int u , int par)
{
	//cout<<u<<" "<<par<<endl;
	dv[u] = low[u] = ++cnt;
	nflag[u] = true;
	for(int i = 0; i < dac[u].size(); i++)
	{
		int v  = dac[u][i].first.second;
		if(!nflag[v])
		{
			dfs(v , u);
			low[u] = min(low[u],low[v]);
		}
		else if(v!=par)
		{
			low[u] = min(low[u],dv[v]);
		}
	}
}


void print(int cur , int old)
{
	if(cur < old)
	{
		cout<<"HAPPY"<<endl;
	}
	else if(old < cur)
	{
		cout<<"SAD"<<endl;
	}
	else
	{
		cout<<"SOSO"<<endl;
	}
}

int main()
{
    int n, m , u , v;
    ll w;
    cin>>n>>m;
    for(int i = 0; i < m; i++)
    {
        cin>>u>>v>>w;
        adj[u].push_back(make_pair( make_pair(w,v) , i));
        radj[v].push_back(make_pair(make_pair(w , u) , i));
        edge.push_back(make_pair(make_pair(u , v) , w));
       mp[make_pair(make_pair(u , v), w)] ++;
    }

    for(int i = 0; i < mxn ; i++)
    {
        dis[i] = inf;
        rdis[i] = inf;
    }
    dks(1 , 2);
	rdks(2 , 1);
	/*for(int i = 1; i <= n; i++)
	{
		cout<<dis[i]<<" ";
	}
	cout<<endl;
	for(int i = 1; i <= n; i++)
	{
		cout<<rdis[i]<<" ";
	}
	cout<<endl;*/
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<adj[i].size(); j++)
        {
		//	cout<<(dis[i]+adj[i][j].first.first) <<" "<<dis[adj[i][j].first.second]<<" "<<rdis[i]<<" "<<rdis[adj[i][j].first.second]+adj[i][j].first.first<<endl;
            if(dis[i]+adj[i][j].first.first==dis[adj[i][j].first.second] && rdis[i]==rdis[adj[i][j].first.second]+adj[i][j].first.first)
                {
					dac[i].push_back(adj[i][j]);
					dac[adj[i][j].first.second].push_back(make_pair(make_pair(adj[i][j].first.first, i) , adj[i][j].second));
					eflag[adj[i][j].second] = true;
				}
        }
    }
   
    memset(nflag , false ,sizeof nflag);
    /*for(int i = 1; i <= n; i++)
    {
		cout<<i<<":";
		for(int j = 0; j < dac[i].size(); j++)
		{
			cout<<dac[i][j].first.second<<" ";
		}
		cout<<endl;
	}*/
    dfs(1 , 0);
    /*for(int i = 1; i <= n; i++)
    {
		cout<<low[i]<<" ";
	}
	cout<<endl; */
	
	for(int i = 0; i < edge.size(); i++)
	{
		if(!eflag[i]) continue;
		int u = edge[i].first.first , v = edge[i].first.second;
		if(dv[u] >= low[v] ) eflag[i] = 0;
		
	}
	for(int i =0; i < edge.size(); i++)
	{
		int u = edge[i].first.first , v = edge[i].first.second ;
		ll w = edge[i].second;
		if(eflag[i])
		{
			ll nw = min( inf , dis[v] + w+ rdis[u]);
			print(nw , dis[2]);  
		}
		else
		{
			ll nw = min(dis[2] , dis[v] + w + rdis[u]);
			print(nw , dis[2]);
		}
	}
    return 0;
}